Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

AP Computer Science A: Deep Dive and FRQ Mastery — Alpha School

Lesson 13: Inheritance, super and Method Overriding

Inheritance lets a new class reuse the code of an existing class, and it is the way Java models 'is-a' relationships: a Dog is an Animal, a SavingsAccount is a BankAccount, a Student is a Person. The new class, the subclass, is declared with extends, as in public class Dog extends Animal, and it inherits the fields and methods of its superclass, adding its own or changing the behaviour of the inherited ones. Inheritance removes duplication: the code that is common to all animals lives once in Animal, and each subclass adds only what makes it different.

A subclass constructor must set up the inherited part of the object, and it does so by calling the superclass constructor with super(...) as its first statement. If the superclass has a constructor that takes arguments, the subclass must call it explicitly with the right values; if the superclass has a no-argument constructor, Java calls it automatically when super is omitted. A subclass can also use super to call an inherited method, as in super.speak(), which is useful when the subclass wants to extend the superclass behaviour rather than replace it entirely.

Method overriding is when a subclass provides its own version of a method inherited from its superclass, with the same name, the same parameter list, and a compatible return type. When the method is called on a subclass object, the subclass version runs, not the superclass version — this is the behaviour that makes polymorphism possible, and the next lesson explores it. Overriding is not the same as overloading, where a class has several methods with the same name but different parameter lists; overloading is decided at compile time by the arguments, while overriding is decided at run time by the object's actual class.

Fields are not overridden the way methods are: a subclass that declares a field with the same name as a superclass field hides the superclass field rather than replacing it, and which one is seen depends on the reference type, not the object's class. This is a subtle point and a favourite exam trap, so keep fields private in the superclass and reach them through methods, which do behave polymorphically. Also note that a subclass inherits only what is accessible: private fields and methods of the superclass exist in the subclass object but cannot be used directly by the subclass code, which must go through public or protected methods instead.

Inheritance should model a genuine is-a relationship, and the is-a test is the way to check it: a Dog is an Animal, so Dog extending Animal is sound, but a Car is not an Engine, so Car should not extend Engine, even though a car has an engine. When the relationship is has-a rather than is-a, the right design is composition — the Car holds an Engine as a field — rather than inheritance. A subclass also inherits the public methods of its superclass, which means it must honour the promises those methods make: if the superclass method guarantees a result, the overriding version must deliver the same guarantee. The protected modifier sits between private and public: protected members are visible to subclasses but not to unrelated classes, and they are the usual way to share implementation details with subclasses while hiding them from everyone else.

A worked example

Worked example: the classes below model animals. Dog inherits the name field and the speak method from Animal, overrides speak with its own version, and its constructor calls super to set the name. Calling speak on a Dog runs the Dog version.

public class Animal
{
    private String name;
    public Animal(String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return name;
    }
    public String speak()
    {
        return "...";
    }
}
public class Dog extends Animal
{
    public Dog(String name)
    {
        super(name);
    }
    public String speak()
    {
        return "Woof";
    }
}
// Dog d = new Dog("Rex"); d.speak() returns "Woof"

The Dog constructor's first statement, super(name), runs the Animal constructor and sets the private name field, which Dog's own code cannot touch directly. The speak method in Dog has the same signature as in Animal, so it overrides it, and d.speak() returns 'Woof' rather than '...'. If Dog had not overridden speak, d.speak() would have run the Animal version and returned '...'. In free-response answers, check the signature of an overriding method carefully: a different parameter list makes it an overload, not an override, and the superclass version will run instead.

Key Terms

Inheritance
A subclass reusing and extending the fields and methods of a superclass.
Subclass
A class declared with extends, inheriting from a superclass.
Superclass
The class being extended; its members are inherited by the subclass.
super(...)
A call to the superclass constructor, which must be the first statement of a subclass constructor.
Overriding
A subclass providing its own version of an inherited method with the same signature.
Overloading
Several methods with the same name but different parameter lists in one class.

Discussion Questions

  1. What is the difference between overriding and overloading?
  2. Why must super(...) be the first statement of a subclass constructor?
  3. Why can a subclass not use a private field of its superclass directly?

Practice Questions

  1. What keyword declares that one class inherits from another?
  2. A subclass constructor does not call super(...). What happens?
  3. A subclass method has the same name as a superclass method but a different parameter list. Is it an override?
  4. How can a subclass method use the superclass version of a method it overrides?
  5. A Dog object is stored in an Animal variable. Which speak method runs when speak() is called?
Show answers
  1. extends
  2. Java inserts a call to the superclass's no-argument constructor; if there is none, it is a compile-time error.
  3. No, it is an overload, and the superclass method is not replaced.
  4. By calling super.methodName(...).
  5. The Dog version, because overriding is decided by the object's actual class at run time.
Keep reading to complete this lesson