Unit 3 · OOP Concepts

3.5.1The extends and super keywords

The keyword extends makes one class a subclass of another. The subclass automatically receives the attributes and methods of the superclass and can add new ones of its own.

Java
class Animal {
    protected String name;

    Animal(String name) { this.name = name; }

    void eat() { System.out.println(name + " is eating"); }
}

class Dog extends Animal {
    Dog(String name) {
        super(name);          // call the parent constructor
    }
    void bark() { System.out.println(name + " says Woof"); }
}

Dog d = new Dog("Rex");
d.eat();    // inherited from Animal
d.bark();   // defined in Dog
Animal# name: String+ Animal(String)+ eat()Dog+ Dog(String)+ bark()Dog is-a Animala Dog object containsfrom Animalname = "Rex"eat()from Dogbark()UML: a hollow triangle points from the subclass to the superclass
Dog extends Animal: a Dog object has the inherited name and eat() plus its own bark()

super(...) must be the first statement in a subclass constructor. If it is omitted, Java inserts a call to the parent's no-argument constructor, and compilation fails if the parent does not have one.

A subclass cannot use the superclass's private members directly (they still exist inside the object); make them protected or use getters.

What exactly is inherited?

Member of the superclassInherited by the subclass?
public and protected attributes and methodsYes, and they can be used directly
Default (package) membersYes, if the subclass is in the same package
private attributes and methodsThey exist inside the subclass object, but the subclass cannot use them directly. It must use public or protected methods (for example getters)
ConstructorsNever inherited. Each class writes its own constructors and calls the parent's with super(...)
static membersCan be used through the subclass, but they still belong to the superclass

The two uses of super

  1. super(...) calls a constructor of the parent class. It must be the first line of the subclass constructor. In Dog(String name) { super(name); }, the Animal constructor sets name, so Dog does not have to repeat that code.
  2. super.method() calls the parent's version of a method, usually from inside a method that overrides it:
Java
class Dog extends Animal {
    Dog(String name) { super(name); }

    @Override
    void eat() {
        super.eat();                                 // Rex is eating
        System.out.println(name + " wags its tail"); // then add Dog's extra
    }
}

This lets the subclass extend the parent's behaviour instead of replacing it completely.

A subclass object is also a superclass object

Because a Dog is an Animal, a Dog object can be used wherever an Animal is expected:

Java
Animal a = new Dog("Rex");   // allowed: every Dog is an Animal
a.eat();                     // allowed: eat() is in Animal
// a.bark();                 // ERROR: the Animal type has no bark()

The variable's type decides which methods you are allowed to call. This is the basis of polymorphism, covered in 3.6.

Inheritance must make sense. Only use extends when the is-a sentence is true. "A Car is an Engine" is false, so Car must not extend Engine, even though a car uses an engine. A Car has an Engine, which is composition (see 4.3).

Remember

  • class B extends A gives B all the non-private members of A; B can add more.
  • Constructors are not inherited; call the parent's constructor with super(...) as the first line.
  • super.method() runs the parent's version of a method.