Unit 3 Cycle 1 Day 11: Inheritance Basics
Share
Inheritance Basics
Section 3.12 — Inheritance Basics
Key Concept
Inheritance allows a subclass to extend a superclass, inheriting all public methods and gaining access to protected members. The subclass extends exactly one superclass (Java does not support multiple inheritance). The subclass can add new fields and methods or override inherited methods. A subclass is-a type of its superclass, meaning a subclass object can be used anywhere a superclass type is expected. On the AP exam, inheritance questions test whether subclass objects correctly inherit behavior and whether method calls resolve to the correct version.
Consider the following classes.
What does new Cat().speak() return?
Answer: (B) "Meow"
Cat overrides speak() from Animal. When called on a Cat object, the Cat version executes, returning "Meow".
Why Not the Others?
(A) The Animal version is overridden by Cat. The Cat version runs.
(C) Overriding replaces the parent method, it does not concatenate.
(D) Cat correctly extends Animal and overrides speak().
Common Mistake
When a subclass overrides a method, calling that method on a subclass object always runs the subclass version. The parent version is replaced, not combined.
AP Exam Tip
Overriding = subclass provides its own version of a parent method with the same signature. The subclass version always wins when called on a subclass object.