Inheritance (beyond the 2026 exam)
- Know that inheritance and polymorphism were removed from the AP CSA exam in the 2025 redesign — this lesson is background, not exam preparation
- Use `extends` to create a subclass that inherits a superclass’s members
- Predict which method runs when a subclass overrides a superclass method
- Call a superclass constructor with `super(...)`
None of this is on the current exam. The 2025 redesign of AP Computer Science A removed inheritance, polymorphism, extends, super and interfaces from the course. If you are revising for the exam, skip this lesson — Unit 3 is now tested on writing classes, instance variables, this, encapsulation, and static members and scope. This is here because inheritance is real Java that you will meet the moment you write code outside this course, and it costs you nothing to read once the exam is over.
extends builds an is-a relationship
Inheritance lets one class build on another. A subclass written class Cat extends Animal automatically has all of Animal’s public methods and can add or change behavior. This models an is-a relationship: a Cat is an Animal. The class being extended is the superclass.
Overriding and polymorphism
A subclass can override a superclass method by declaring a method with the same signature. When you call that method, Java runs the version belonging to the object’s actual type at runtime — this is polymorphism. So Animal a = new Cat(); followed by a.speak() runs Cat’s speak, even though the variable’s declared type is Animal.
Calling the superclass constructor with super
A subclass constructor can invoke its superclass’s constructor with super(args), which must be the first statement. In class Car extends Vehicle { public Car() { super(4); } }, the Car constructor calls Vehicle(int w) with 4, so the inherited wheels field is set even though Car did not declare it. Inherited methods like getWheels() then work on the subclass object.
Given class Animal { public String speak() { return "..."; } } and class Cat extends Animal { public String speak() { return "Meow"; } }, trace: Animal a = new Cat(); then System.out.println(a.speak());
- 1.
new Cat()builds a Cat object; the variableais declared asAnimalbut the object is a Cat. - 2.At runtime,
a.speak()uses the object’s actual type, Cat. - 3.Cat overrides
speak()to return"Meow", so that version runs.
Meow. Polymorphism runs the Cat override, not the Animal version, because the object is a Cat.Given `class Animal { public String speak() { return "..."; } }` and `class Cat extends Animal { public String speak() { return "Meow"; } }`, what does this print? `Animal a = new Cat();` then `System.out.println(a.speak());`
The declared type (Animal) decides which methods you may call, but the actual object type (Cat) decides which overridden version runs. Do not assume the declared type’s method executes.
Given `class Vehicle { private int wheels; public Vehicle(int w) { wheels = w; } public int getWheels() { return wheels; } }` and `class Car extends Vehicle { public Car() { super(4); } }`, what does this print? `Car c = new Car();` then `System.out.println(c.getWheels());`
A subclass inherits public methods but not private fields directly — it reaches them through inherited methods or by passing values up with super(...). super(...) must be the first line of the subclass constructor.
Answer the 2 checkpoints as you read.
Sign in to save your progress