← Back to course

Inheritance (beyond the 2026 exam)

You’ll be able to
On the exam

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.

Subclass with super call
class Sub extends Super { Sub(){ super(args); } }
`super(args)` runs the superclass constructor first, initializing inherited fields before the subclass adds its own setup.
Worked example

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. 1.new Cat() builds a Cat object; the variable a is declared as Animal but the object is a Cat.
  2. 2.At runtime, a.speak() uses the object’s actual type, Cat.
  3. 3.Cat overrides speak() to return "Meow", so that version runs.
Answer: It prints Meow. Polymorphism runs the Cat override, not the Animal version, because the object is a Cat.
Checkpoint

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());`

Watch out

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.

Checkpoint

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());`

On the exam

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