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.
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 Dogsuper(...) 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 superclass | Inherited by the subclass? |
|---|---|
public and protected attributes and methods | Yes, and they can be used directly |
| Default (package) members | Yes, if the subclass is in the same package |
private attributes and methods | They exist inside the subclass object, but the subclass cannot use them directly. It must use public or protected methods (for example getters) |
| Constructors | Never inherited. Each class writes its own constructors and calls the parent's with super(...) |
static members | Can be used through the subclass, but they still belong to the superclass |
The two uses of super
super(...)calls a constructor of the parent class. It must be the first line of the subclass constructor. InDog(String name) { super(name); }, the Animal constructor setsname, so Dog does not have to repeat that code.super.method()calls the parent's version of a method, usually from inside a method that overrides it:
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:
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 Agives 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.