AP Computer Science A Unit 9: Inheritance
Study superclass/subclass, polymorphism, abstract classes, Object class with exam-format practice and rubric-based scoring.
Start AP CSA Practice · Full Study Guide
AP and Advanced Placement are trademarks of College Board. AimFive is not affiliated with or endorsed by College Board.
Inside This Unit: The Full Breakdown
Inheritance allows a subclass to extend a superclass, inheriting its methods and fields while adding or overriding behavior. This unit covers the is-a relationship, method overriding, and polymorphism.
Why it matters
Inheritance and polymorphism are core OOP concepts tested extensively on the AP CSA exam. The free-response section regularly asks you to write subclasses, override methods, and trace polymorphic method calls.
Key concepts
- A subclass extends a superclass using the extends keyword and inherits all public methods.
- Constructors are not inherited; subclass constructors call super() to invoke the superclass constructor.
- Overriding replaces a superclass method with a subclass version that has the same signature.
- Polymorphism: a superclass reference can hold a subclass object, and the overridden method is called at runtime.
Inheritance Basics
Inheritance models an is-a relationship: a Dog is an Animal, so Dog extends Animal. The subclass inherits all public instance methods and can access inherited methods directly. Private instance variables of the superclass are not directly accessible — the subclass must use inherited public methods (getters/setters) to access them. A subclass can add new instance variables and methods that do not exist in the superclass, specializing the behavior for its particular type.
Constructors and super
Subclass constructors must call a superclass constructor as the first statement using super(arguments). If you do not write an explicit super call, Java automatically inserts super() (no-argument). If the superclass has no no-argument constructor, you must explicitly call super with the required arguments or you get a compile error. The super keyword can also call superclass methods from within the subclass: super.methodName() calls the superclass version of an overridden method.
Polymorphism and Method Overriding
A method is overridden when a subclass defines a method with the same name, return type, and parameter list as a superclass method. At runtime, Java calls the actual object's version, not the reference type's version — this is polymorphism. If Animal has a speak() method and Dog overrides it, calling animal.speak() on a Dog object runs Dog's version. This works with ArrayLists: ArrayList<Animal> can hold Dog and Cat objects, and each calls its own speak(). The AP exam tests tracing polymorphic calls with specific object types.
AP exam tip
On the AP exam, when tracing a method call on a polymorphic reference, always ask: what is the actual type of the object (not the reference type)? That determines which overridden method executes.
Connections to other units
- Unit 4: Writing classes provides the foundation — inheritance extends those classes with new behavior.
- Unit 6: ArrayLists of superclass types store mixed subclass objects, enabling polymorphic processing.
- Unit 10: Recursive methods can be overridden in subclasses, combining both concepts.