Unit 9: Inheritance
AP Computer Science A: 48 practice questions with detailed explanations.
Unit Study Guide
Executive Summary
Inheritance lets a subclass reuse a superclass's public members and override methods to specialize behavior.
Extending classes
class Dog extends Animal means Dog is-a Animal. The subclass inherits public methods and variables but NOT private members. Every class extends Object, gaining toString() and equals().
Overriding vs overloading
Overriding redefines an inherited method with the SAME signature; the subclass version runs for subclass objects. Overloading is same name, different parameters, in the same class. An override cannot reduce access (public to private fails).
super
super.method() calls the parent's version from inside an override. super(args) must be the first statement of a constructor; if omitted, Java inserts super() automatically.
Polymorphism
Animal a = new Dog() is legal upcasting. Calling a.speak() dispatches to Dog's version at runtime (dynamic dispatch). What you can CALL is limited by the reference type (Animal), but what RUNS is chosen by the object's class. Static methods, by contrast, bind to the declared type.
Exam traps
A superclass reference cannot call subclass-only methods. Constructors run parent-first. private members are not inherited. Final classes cannot be extended.