Inheritance: extends, super, and Method Overriding

AP Computer Science A · Unit 3 — Class Creation

extends says is-a

public class Manager extends Employee declares that every Manager is an Employee. The subclass inherits all public methods of the superclass. It does not inherit constructors, and although every subclass object still carries the superclass private instance variables, it cannot touch them by name — it must go through the superclass accessors and mutators.

Constructors run outward-in

The first statement of every subclass constructor is a call to a superclass constructor. If you write super(...) explicitly, that one runs; if you write nothing, Java inserts super() with no arguments. So if the superclass has no no-argument constructor and you forget the explicit super(...), the code does not compile. Execution order is therefore superclass constructor first, then the subclass constructor body.

Overriding versus overloading

Overriding replaces an inherited method: same name, same parameter list, in a subclass. Overloading adds a new method: same name, different parameter list, usually in the same class. Change a parameter type by accident and you have overloaded rather than overridden — the superclass version still runs, and nothing warns you.

Inside an override, super.methodName(args) calls the superclass version, which is the standard way to say do everything the parent did, then add my part.

Employee Employee(String n) Manager Manager(String n, int t) 1. super(n) goes UP 2. bodies run DOWN new Manager("An", 4) Employee body prints first, Manager body prints after superclass has no no-argument constructor and you forget super(n) = compile error
The two arrow directions answer two different questions. The blue upward arrow answers which constructor is called: the call climbs all the way up to the superclass before any body runs. The red downward arrow answers the print order: the superclass body finishes first, and only then does the subclass body run. Exams often put a System.out.print statement in each of two constructors and ask you to predict the order — draw these two arrows correctly and you are done.

Worked example: super in two roles: calling the constructor and calling the superclass version

Given a class Employee with a name and a base salary and a method pay() that returns the pay. Write a class Manager that inherits from it, adds the number of employees managed, and whose pay() equals the base salary plus 200000 for each employee. You must not copy the parent's pay formula.

Solution

public class Employee {
    private String name;
    private int base;

    public Employee(String name, int base) {
        this.name = name;
        this.base = base;
    }

    public int pay() {
        return base;
    }

    public String getName() {
        return name;
    }
}

public class Manager extends Employee {
    private int team;

    public Manager(String name, int base, int team) {
        super(name, base);
        this.team = team;
    }

    public int pay() {
        return super.pay() + 200000 * team;
    }
}

Four points worth noting:

Common mistake

A failed override is the most costly trap in this problem. The superclass has public void add(int n), and the subclass types public void add(double n) — the compiler treats this as a new method (overloading); the superclass version remains intact and is still the one called when you pass an integer. The program runs with the old result without any error. How to prevent it: when overriding, copy the superclass declaration line exactly and only then change the body — do not retype it from memory. A sibling trap: calling pay() instead of super.pay() inside the overriding pay() itself creates infinite recursion.

Key takeaway

extends = is a. Constructors are not inherited, and super(...) must be on the first line. The superclass runs first, the subclass second. Same name + same parameters = overriding; same name + different parameters = overloading. A superclass's private variable can be read by the subclass only by borrowing an accessor.

← Accessors, Mutators, and the static Keyword · All AP Computer Science A lessons · Practice AP Computer Science A questions (free, instant scoring) · Polymorphism, the Object Class, and is-a versus has-a →