CS 2110 Recitation Notes
Lecture 5: Inheritance
Subclasses
A subclass is a child of another class.
Example:
public class InterestAccount extends Account
- InterestAccount is a subclass of Account
- Account is the superclass of InterestAccount
<aside>
💡 A class has at most one direct superclass
</aside>
A class hierarchy diagram shows how subclasses point to parent classes.
- Extension should model the real world.
- A should extend B if and only if A "is a " B.
Inheritance
- A subclass inherits the fields and methods of its superclass
- A subclass can override methods of its superclass
- They start with all the contents of their parents class, then they add a partition at the bottom for the subclass.
- Private fields are off limits to subclasses!
- Bottom up rule: Start at the bottom of a folder and work upward to find a method
The $\texttt{super}$ keyword invokes a method of a class's parent using the bottom up rule.
Overriding
The $\texttt{@Override}$ annotation indicates the author's intent to override a method so it's not just accidental.