Ch 9
Chapter 9: Inheritance
Chapter Goals
Understand inheritance concepts.
Implement subclasses that inherit and override superclass methods.
Learn about polymorphism in Java.
Familiarize with the common superclass, Object, and its methods.
Inheritance Hierarchies
Definition of Inheritance
Inheritance describes the relationship between a general class (superclass) and a specialized class (subclass).
A subclass inherits data and behaviors from the superclass.
Example: Cars share common traits of all vehicles.
Vehicle Example Hierarchy
Superclass: Vehicle
Subclasses: Motorcycle, Car (which includes Truck, Sedan, SUV)
Shared trait: Transporting people.
Code Reusability via Inheritance
Inheritance allows code reuse, reducing duplication.
A subclass inherits methods from its superclass.
A subclass object can replace a superclass object without altering the program's logic (substitution principle).
Example of Substitution Principle
A method processing Vehicle objects can handle any specific vehicle type, e.g., Car.
Example: Question Hierarchy
Question Class Structure
Different question types (standard and computer-graded quiz questions) can be implemented with inheritance.
Common method functionalities include displaying text and checking answers.
Implementing Subclasses
A subclass is formed by modifying an existing class.
Example: ChoiceQuestion as a subclass of Question.
Inherits all methods from the superclass.
Only the new or different instance variables and methods are declared.
Choice Question Characteristics
Stores various answer choices.
Adds methods to handle these answer choices and to display them to the user.
Subclass Declaration
Syntax Overview
public class SubclassName extends SuperclassName {
// instance variables
// methods
}extendsindicates inheritance of the subclass from its superclass.
Method Inheritance
Access to Superclass Methods
A subclass method can utilize public methods of the superclass. Private instance variables, however, remain inaccessible.
Example of adding methods:
public void addChoice(String choice, boolean correct)in ChoiceQuestion.
Overriding Methods
If inherited behavior is unsuitable, it can be overridden by defining a new implementation in the subclass.
The
superkeyword is used to refer to superclass methods within the subclass.
public void display() {
super.display(); // Calls the display method of the superclass
}Polymorphism
Understanding Polymorphism
Allows the same method to behave differently based on the object it is called on.
A superclass reference can refer to subclass objects.
Example:
public static void presentQuestion(Question q)can be used with both Question and ChoiceQuestion objects.
Common Errors in Inheritance
Mistakes to Avoid
Replicating Instance Variables: Don't declare the same instance variable in the subclass.
Correct approach: Use inherited methods to access superclass variables.
Overriding Needs: Ensure matching parameter types when overriding, or else a new method is created (overloading).
Using super Keyword: Forgetting to use
superwhen invoking a superclass method can lead to errors.
Object Class in Java
Every class in Java is a subclass of the Object class unless explicitly stated otherwise.
Common Object class methods:
toString(),equals(), andhashCode().
Overriding Object Methods
toString() Method
Provides a string representation of the object, useful for debugging.
equals() Method
Compares content of two objects; must be overridden to provide meaningful equality checks.
Using the instanceof Operator
Allows checking if an object is of a certain type before casting to prevent exceptions.
Self-Check Questions
Practice questions allowing self-assessment of understanding about inheritance and polymorphism.