Inheritance
Notes
- Inheritance is the process of deriving a new class from an existing one.
- One purpose of inheritance is to reuse existing software.
- The original class is called the parent class, superclass, or base class.
- Inheritance creates an “is-a” relationship between the parent and child classes.
- Java uses the reserved word extends to indicate that a new class is being derived from an existing class.
- Inheritance is a one-way street.
An example:
A UML class diagram showing an inheritance relationship
public class Words { //----------------------------------------------------------------- // Instantiates a derived class and invokes its inherited and // local methods. //----------------------------------------------------------------- public static void main (String[] args) { Dictionary webster = new Dictionary(); System.out.println ("Number of pages: " + webster.getPages()); System.out.println ("Number of definitions: " + webster.getDefinitions()); System.out.println ("Definitions per page: " + webster.computeRatio()); } }
public class Book { protected int pages = 1500; //---------------------------------------------------------------- // Pages mutator. //---------------------------------------------------------------- public void setPages (int numPages) { pages = numPages; } //---------------------------------------------------------------- // Pages accessor. //---------------------------------------------------------------- public int getPages () { return pages; } }
public class Dictionary extends Book { private int definitions = 52500; //----------------------------------------------------------------- // Prints a message using both local and inherited values. //----------------------------------------------------------------- public double computeRatio () { return (double) definitions/pages; } //---------------------------------------------------------------- // Definitions mutator. //---------------------------------------------------------------- public void setDefinitions (int numDefinitions) { definitions = numDefinitions; } //---------------------------------------------------------------- // Definitions accessor. //---------------------------------------------------------------- public int getDefinitions () { return definitions; } }
Visibility Modifiers
- Private methods and variables of the parent class cannot be referenced in the child class or through an object of the child class.
- Public visibility allows a derived class to reference it, but violates the principle of encapsulation.
- Protected visibility allows the class to retain some encapsulation properties.
- Protected visibility provides the best possible encapsulation that permits inheritance. However, the encapsulation is not as tight as if the variable or method were declared private, but it is better than if it were declared public.
- Modifiers list:
- public – visible anywhere
- protected – can only be applied to inner classes. Visible to any classes in the package and to children classes.
- private – can only be applied to inner classes. Visible enclosing class only.
- no modifier – default – visible to any classes in the same package.
- For the AP Computer Science A Exam a design question may required a student to develop a solution that includes all data declared private.
The Super keyword
- The reserved word super can be used in a class to refer to its parent class.
- Using the super reference, we can access a parent’s members.
- Like the this reference, what the word super refers to depends on the class in which it is used.
- A parent’s constructor can be invoked using the super reference. ( First line in the constructor )
- If no such call exists, Java will automatically make a call to super() at the beginning of the constructor.
- The super reference can also be used to reference other variables and methods defined in the parent’s class.
- The visibility modifiers determine whether a variable or method is inherited into a subclass. If a variable or method is inherited, it can be referenced directly in the child class by name, as if it were declared locally in the child class.
- However, all variables and methods in a parent class exist for an object of a child class, even though they can’t be referenced directly. They can, however, be referenced indirectly.
- All members of a superclass exist for a subclass, but they are not necessarily inherited. Only inherited members can be referenced by name in the subclass.
public class Words2 { //----------------------------------------------------------------- // Instantiates a derived class and invokes its inherited and // local methods. //----------------------------------------------------------------- public static void main (String[] args) { Dictionary2 webster = new Dictionary2 (1500, 52500); System.out.println ("Number of pages: " + webster.getPages()); System.out.println ("Number of definitions: " + webster.getDefinitions()); System.out.println ("Definitions per page: " + webster.computeRatio()); } }
public class Book2 { protected int pages; //---------------------------------------------------------------- // Constructor: Sets up the book with the specified number of // pages. //---------------------------------------------------------------- public Book2 (int numPages) { pages = numPages; } //---------------------------------------------------------------- // Pages mutator. //---------------------------------------------------------------- public void setPages (int numPages) { pages = numPages; } //---------------------------------------------------------------- // Pages accessor. //---------------------------------------------------------------- public int getPages () { return pages; } }
public class Dictionary2 extends Book2 { private int definitions; //----------------------------------------------------------------- // Constructor: Sets up the dictionary with the specified number // of pages and definitions. //----------------------------------------------------------------- public Dictionary2 (int numPages, int numDefinitions) { super(numPages); definitions = numDefinitions; } //----------------------------------------------------------------- // Prints a message using both local and inherited values. //----------------------------------------------------------------- public double computeRatio () { return (double) definitions/pages; } //---------------------------------------------------------------- // Definitions mutator. //---------------------------------------------------------------- public void setDefinitions (int numDefinitions) { definitions = numDefinitions; } //---------------------------------------------------------------- // Definitions accessor. //---------------------------------------------------------------- public int getDefinitions () { return definitions; } }
Overriding Notes
- A child class can override (redefine) the parent’s definition of an inherited method.
- Constructors, however, are not inherited and cannot be overridden.
- Method overriding is a key element in object-oriented design.
- Method overriding allows two objects that are related by inheritance to use the same naming conventions for methods that accomplish the same general task in different ways.
- A method can be defined with the final modifier. A child class cannot override a final method. This technique is used to ensure that a derived class uses a particular definition of a method.
- The final modifier can also be applied to an entire class. A final class cannot be extended at all.
Shadowing
- If a variable of the same name is declared in a child class, it is called a shadow variable.
- To avoid confusion and logical errors, shadowing variables should be avoided.
Class Hierarchy
A UML class diagram showing a class hierarchy
- The child of one class can be the parent of one or more other classes, creating a class hierarchy.
- Common features should be located as high in a class hierarchy as is reasonably possible.
- All Java classes are derived, directly or indirectly, from the Object class.
- The toString and equals methods are inherited by every class in every Java program.
- Inheritance can be applied to interfaces so that one interface can be derived from another.
- Private members are inherited by the child class, but cannot be referenced directly by name. They may be used indirectly, however.
- Java’s approach to inheritance is called single inheritance.
Abstract Classes
- It represents a concept on which other classes can build their definitions.An abstract class cannot be instantiated.
- A class derived from an abstract parent must override all of its parent’s abstract methods, or the derived class will also be considered abstract.
- An abstract class is similar to an interface in some ways.
- However, unlike interfaces, an abstract class can contain methods that are not abstract.
- An abstract class can also contain data declarations other than constants.
- A class is declared as abstract by including the abstract modifier in the class header.
- Any class that contains one or more abstract methods must be declared as abstract.
- In abstract classes (unlike interfaces), the abstract modifier must be applied to each abstract method.
- A class declared as abstract does not have to contain abstract methods.
- Abstract classes serve as placeholders in a class hierarchy.
- If a child of an abstract class does not give a definition for every abstract method that it inherits from its parent, then the child class is also considered abstract.
- Note that it would be a contradiction for an abstract method to be modified as final or static.
- Because abstract methods have no implementation, an abstract static method would make no sense.
Homework:
Read pages 382 through 419
All source: Java Software Solutions by Lewis, Loftus and Cocking