Notes
Outline
Chapter 7:  Inheritance
Presentation slides for
Java Software Solutions
Foundations of Program Design
Third Edition
by John Lewis and William Loftus
Java Software Solutions is published by Addison-Wesley
Presentation slides are copyright 200s by John Lewis and William Loftus. All rights reserved.
Instructors using the textbook may use and modify these slides for pedagogical purposes.
Inheritance
Another fundamental object-oriented technique is called inheritance, for organizing and creating classes and for promoting  reuse
Chapter 7 focuses on:
deriving new classes from existing classes
creating class hierarchies
the protected modifier
polymorphism via inheritance
inheritance hierarchies for interfaces
inheritance used in graphical user interfaces
Inheritance
Inheritance allows a software developer to derive a new class from an existing one
The existing class is called the parent class, or superclass, or base class
The derived class is called the child class or subclass.
As the name implies, the child inherits characteristics of the parent
That is, the child class inherits the methods and data defined for the parent class
Inheritance
To tailor a derived class, the programmer can add new variables or methods, or can modify the inherited ones
Software reuse is at the heart of inheritance
By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software
Inheritance
Inheritance relationships often are shown graphically in a class diagram, with the arrow pointing to the parent class
Deriving Subclasses
In Java, we use the reserved word extends to establish an inheritance relationship
class Car extends Vehicle
{
   // class contents
}
See Words.java (page xxx)
See Book.java (page xxx)
See Dictionary.java (page xxx)
Words.java
Book.java
Dictionary.java
The Book and Dictionary Classes
(Figure 7.1 here)
The protected Modifier
Visibility modifiers determine which class members are inherited and which are not
Variables and methods declared with public visibility are inherited; those with private visibility are not
But public variables violate the principle of encapsulation
There is a third visibility modifier that helps in inheritance situations: protected
The protected Modifier
The protected visibility modifier allows a member of a base class to be inherited into a child
protected visibility provides more encapsulation than public does
However, protected visibility is not as tightly encapsulated as private visibility
The details of each modifier are given in Appendix XXX
The super Reference
Constructors are not inherited, even though they have public visibility
Yet we often want to use the parent's constructor to set up the "parent's part" of the object
The super reference can be used to refer to the parent class, and often is used to invoke the parent's constructor
(note to editor: there is an _ preceding each “(page” below that I am unable to eliminate)
See Words2.java (page xxx)
See Book2.java (page xxx)
See Dictionary2.java (page xxx)
Words2.java
Book2.java
Dictionary2.java
The super Reference
A child’s constructor is responsible for calling the parent’s constructor
The first line of a child’s constructor should use the super reference to call the parent’s constructor
The super reference can be used to reference other variables and methods defined in the parent’s class
Single vs. Multiple Inheritance
Java supports single inheritance, meaning that a derived class can have only one parent class
Multiple inheritance, in some other languages, allows a class to be derived from two or more classes, inheriting the members of all parents
Collisions, such as the same variable name in two parents, have to be resolved
In most cases, the use of interfaces gives us aspects of multiple inheritance without the overhead
Overriding Methods
A child class can override the definition of an inherited method in favor of its own
That is, a child can redefine a method that it inherits from its parent
The new method must have the same signature as the parent's method, but can have a different body
The type of the object executing the method determines which version of the method is invoked
Overriding Methods
(Editor: same “_” problem described on slide 13)
See Messages.java (page xxx)
See Thought.java (page xxx)
See Advice.java (page xxx)
Messages.java
Thought.java
Advice.java
Overriding Methods and Variables
Note that a parent method can be invoked explicitly using the super reference
If a method is declared with the final modifier, it cannot be overridden
The concept of overriding can be applied to data (called shadowing variables), but generally it should be avoided
Overloading vs. Overriding
Don't confuse the concepts of overloading and overriding
Overloading deals with multiple methods in the same class with the same name but different signatures
Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature
Overloading lets you define a similar operation in different ways for different data
Overriding lets you define a similar operation in different ways for different object types
Class Hierarchies
A child class of one parent can be the parent of another child, forming a class hierarchy
Class Hierarchies
Two children of the same parent are called siblings
Good class design puts all common features as high in the hierarchy as is reasonable
An inherited member is passed continually down the line
The inheritance mechanism is transitive.
That is, a child class inherits from all its ancestor classes
Class Hierarchies
There is no single class hierarchy that is appropriate for all situations
Class hierarchies often need to be extended and modified to keep up with changes
The Object Class
A class called Object is defined in the java.lang package of the Java standard class library
All classes are derived from the Object class
If a class is not explicitly defined to be the child of an existing class, it is assumed to be the child of the Object class
Therefore, the Object class is the ultimate root of all class hierarchies
The Object Class
The Object class contains a few useful methods, which are inherited by all classes
For example, the toString method is defined in the Object class
Every time we have defined toString, we have actually been overriding an existing definition
The toString method in the Object class is defined to return a string that contains the name of the object’s class together with other information
The Object Class
That’s why the println method can call toString for any object that is passed to it – all objects are guaranteed to have a toString method via inheritance
(Editor: same “_” problem here)
See Academia.java (page xxx)
See Student.java (page xxx)
See GradStudent.java (page xxx)
Academia.java
Student.java
GradStudent.java
The object Class
The equals method of the Object class determines if two references are aliases
We can override equals to define equality in some more appropriate way
Abstract Classes
An abstract class is a placeholder in a class hierarchy that represents a generic concept
An abstract class cannot be instantiated
We  use the modifier abstract on the class header to declare a class as abstract
Abstract Classes
An abstract class often contains abstract methods with no definitions (like an interface does), though it doesn’t need to
Unlike an interface, the abstract modifier must be applied to each abstract method
An abstract class typically contains non-abstract methods with method bodies, further distinguishing abstract classes from interfaces
A class declared as abstract does not need to contain abstract methods
Abstract Classes
The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract
An abstract method cannot be defined as final (because it must be overridden) or static (because it has no definition yet)
The use of abstract classes is a design decision; it helps us establish common elements in a class that is too general to instantiate
References and Inheritance
An object reference can refer to an object of its class, or to an object of any class related to it by inheritance
For example, if the Holiday class is used to derive a child class called Christmas, then a Holiday reference could be used to point to a Christmas object
References and Inheritance
Assigning a predecessor object to an ancestor reference is considered to be a widening conversion, and can be performed by simple assignment
Assigning an ancestor object to a predecessor reference can be done also, but it is considered to be a narrowing conversion and must be done with a cast
The widening conversion is the most useful
Carrying this to the limit, an Object reference can be used to refer to any object
An ArrayList is designed to hold Object references
Indirect Use of Noninherited Members
An inherited member can be referenced directly by name in the child class, as if it were declared in the child class
But even if a method or variable is not inherited by a child, it can still be accessed indirectly through parent methods
(Editor - same “_” problem)
See FoodAnalysis.java (page xxx)
See FoodItem.java (page xxx)
See Pizza.java (page xxx)
FoodAnalysis.java
FoodItem.java
Pizza.java
Polymorphism
A reference can be polymorphic, which can be defined as "having many forms"
That line of code might execute different methods at different times if the object that it points to changes
Polymorphic references are resolved at run time; this is called dynamic binding
Careful use of polymorphic references can lead to elegant, robust software designs
Polymorphism via Interfaces
An interface name can be used as the type of an object reference variable
Doable obj;
The obj reference can be used to point to any object of any class that implements the Doable interface
The version of doThis that the following line invokes depends on the type of object that obj is referring to
obj.doThis();
Polymorphism via Inheritance
Recall that a polymorphic reference is one which can refer to different types of objects at different times
Inheritance also can be used as a basis of polymorphism
An object reference can refer to one object at one time, and then it can be changed to refer to another object (related by inheritance) at another time
Polymorphism via Inheritance
Suppose the Holiday class has a method called celebrate, and the Christmas class overrides it
Now consider the following invocation:
day.celebrate();
If day refers to a Holiday object, it invokes the Holiday version of celebrate;  if it refers to a Christmas object, it invokes the Christmas version
Polymorphism via Inheritance
It is the type of the object being referenced, not the reference type, that determines which method is invoked
Note that, if an invocation is in a loop, a particular line of code can execute different methods at different times
Polymorphic references are resolved at run-time, not during compilation
Polymorphism via Inheritance
Consider the following class hierarchy:
Polymorphism via Inheritance
Now consider the task of paying all employees
See Firm.java (page xxx)
See Staff.java (page xxx)
See StaffMember.java (page xxx)
See Volunteer.java (page xxx)
See Employee.java (page xxx)
See Executive.java (page xxx)
See Hourly.java (page xxx)
Firm.java
Staff.java
StaffMember.java
Volunteer.java
Employee.java
Executive.java
Hourly.java
Interface Hierarchies
Inheritance can be applied to interfaces as well as to classes
One interface can be derived from another interface
The child interface inherits all abstract methods of the parent
A class implementing the child interface must define all methods from both the ancestor and child interfaces
All members of an interface are public
Note that class hierarchies and interface hierarchies are distinct (they do not overlap)
Polymorphism via Interfaces
An interface name can be used to declare an object reference variable
Interfaces allow polymorphic references in which the method that is invoked is determined by the object being referenced
(Editor: same “_” problem)
See Speaker.java (page xxx)
See Philosopher.java (page xxx)
See Dog.java (page xxx)
Speaker.java
Philosopher.java
Dog.java
Polymorphism via Interfaces
A class can implement multiple interfaces
The interfaces are listed in the implements clause, separated by commas
The class must implement all methods in all interfaces listed in the header
Inheritance and GUIs
An applet is an excellent example of inheritance
Recall that when we define an applet, we extend the Applet class or the JApplet class
The Applet and JApplet classes already handle all the details about applet creation and execution, including the interaction with a Web browser
Inheritance and GUIs
Our applet classes have to deal only with issues that specifically relate to what our particular applet will do
When we define certain methods, such as the paint method of an applet, we are actually overriding a method defined in the Component class, which is ultimately inherited into the Applet class or the JApplet class
GUI Components
A GUI component is an object that represents a visual entity in an graphical user interface (such as a button or a text field)
Components can generate events to which listener objects can respond
For example, an applet is a component that can generate mouse events
An applet is also a special kind of component, called a container, in which other components can be placed
The Component Class Hierarchy
The Java classes that define GUI components are part of a class hierarchy
Swing GUI components typically are derived from the JComponent class which is derived from the Container class which is derived from the Component class
Many Swing components can serve as (limited) containers, because they are derived from the Container class
The Component Class Hierarchy
(Figure 7.9 here)
Mouse Events and Mouse Motion Events
A mouse can generate mouse events and mouse motion events
(Figure 7.10 here)
Mouse Events and Mouse Motion Events
What we listen for depends on our objectives
See Dots.java (page xxx)
See DotsPanel.java (page xxx)
An ArrayList object is used to store the objects so they can be redrawn as necessary
Dots.java
DotsPanel.java
Mouse Events and Mouse Motion Events
Each time the repaint method is called on an applet, the window is cleared prior to calling paint
Empty methods must be provided for unused events
See RubberLines.java (page xxx)
See RubberLinesPanel.java (page xxx)
RubberLines.java
RubberLinesPanel.java
Extending Event Adapter Classes
Listener classes can be created by implementing a particular interface (such as MouseListener interface)
A listener also can be created by extending an event adapter class
Each listener interface has a corresponding adapter class (such as the MouseAdapter class)
Each adapter class implements the corresponding listener and provides empty method definitions
Empty definitions for unused methods need not be provided
Extending Event Adapter Classes
When we derive a listener class from an adapter class, we override any event methods of interest (such as the mouseClicked method)
Note that this avoids the need to create empty definitions for unused events
(Editor: same “_” problem)
See OffCenter.java (page xxx)
See OffCenterPanel.java (page xxx)
OffCenter.java
OffCenterPanel.java
Summary
Chapter 7 has focused on:
deriving new classes from existing classes
creating class hierarchies
the protected modifier
polymorphism via inheritance
inheritance hierarchies for interfaces
inheritance used in graphical user interfaces