Unit 3 Cycle 2 Day 7: I/II/III: Inheritance Rules
Share
I/II/III: Inheritance Rules
Section 3.12 — Inheritance Basics
Key Concept
I/II/III inheritance rules questions test core principles: a subclass inherits all public methods, cannot access private superclass fields directly, must call super() in constructors, can override but not overload through super, and has an is-a relationship with its superclass. Each statement asserts a rule or its violation. Verify each by checking against Java's inheritance specifications. The AP exam especially tests edge cases around private access and constructor requirements.
Consider a class hierarchy where Dog extends Animal.
Which of the statements are true?
Answer: (A) I and III only
I: TRUE. A subclass object can be assigned to a parent variable (upcasting). Animal a = new Dog() is valid.
II: FALSE. A parent object cannot be assigned to a subclass variable without casting. Dog d = new Animal() is a compile error.
III: TRUE. A subclass inherits all public methods from its parent.
Why Not the Others?
(B) II is false. You cannot assign a parent object to a child variable (downcasting requires explicit cast and may fail).
(C) III is also true. Inheritance provides access to all public parent methods.
(D) II is false. Upcasting (child to parent) is automatic, but downcasting is not.
Common Mistake
Upcasting (Dog to Animal) is always safe and automatic. Downcasting (Animal to Dog) requires explicit casting and can fail at runtime with ClassCastException.
AP Exam Tip
The "is-a" relationship: a Dog IS an Animal (upcasting ok). But an Animal is NOT necessarily a Dog (downcasting dangerous).