Unit 3 Cycle 1 Day 25: Inheritance: Method Availability
Share
Inheritance: Method Availability
Section 3.12 — Inheritance Basics
Key Concept
Inheritance determines which methods are available to a subclass, but private members of the superclass are not directly accessible. A subclass inherits all public and protected methods but cannot access private instance variables directly — it must use inherited getters and setters. The AP exam tests whether a method call on a subclass object is valid by checking: is the method defined or inherited? A method defined only in the subclass cannot be called through a superclass reference without casting.
Consider the following hierarchy.
Which methods can be called on a Washer object?
Answer: (B) turnOn(), turnOff(), and wash()
Washer inherits turnOn() and turnOff() from Appliance and adds its own wash(). A subclass has all parent methods plus its own.
Why Not the Others?
(A) Washer also inherits turnOn() and turnOff() from Appliance.
(C) Washer has its own wash() method in addition to inherited methods.
(D) No casting is needed. The Washer object has direct access to all three methods.
Common Mistake
A subclass inherits ALL public methods from its parent. It can also add new methods. The subclass has the union of parent methods and its own methods.
AP Exam Tip
Inheritance is additive: subclasses gain parent methods and can add more. They can also override inherited methods to change behavior.