Java Inheritance

Inheritance lets a class (the subclass or child) take the fields and methods of another class (the superclass or parent). It gives you code reuse and an “is a” relation: a Dog is an Animal.

extends

class Animal {
    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!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog("Tommy");
        d.eat();       // inherited from Animal
        d.bark();      // own method
    }
}
Output

Tommy is eating
Tommy says Woof!

Method overriding

A subclass can give its own version of a method of the parent. Use the @Override annotation, so the compiler checks it.

class Animal {
    void sound() {
        System.out.println("Some sound");
    }
}

class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("Meow");
    }
}

class Cow extends Animal {
    @Override
    void sound() {
        super.sound();               // call the parent version too
        System.out.println("Moo");
    }
}

public class Over {
    public static void main(String[] args) {
        new Cat().sound();
        new Cow().sound();
    }
}
Output

Meow
Some sound
Moo

Constructor order

The parent constructor always runs first.

class A {
    A() { System.out.println("A constructor"); }
}

class B extends A {
    B() { System.out.println("B constructor"); }
}

class C extends B {
    C() { System.out.println("C constructor"); }
}

public class Order {
    public static void main(String[] args) {
        new C();
    }
}
Output

A constructor
B constructor
C constructor

Rules of inheritance

  • Java supports single inheritance for classes: one class can extend only one class. (Use interfaces for more, see Java Interfaces.)
  • Every class extends Object if you write nothing. That is where toString() and equals() come from.
  • private members are not inherited for direct use. Use protected or getters.
  • final on a class stops inheritance. final on a method stops overriding.
final class Base { }

class Child extends Base { }

public class Final {
    public static void main(String[] args) { }
}
Output

Final.java:3: error: cannot inherit from final Base
class Child extends Base { }
^
1 error
error: compilation failed

Is-a or has-a?

Use inheritance only for a real “is a” relation. For “has a”, put an object inside a class (composition): a Car has an Engine, it is not an engine. Composition is more flexible, and many experts say: prefer composition over inheritance.

Note: Use instanceof to check the real type of an object at run time. You will see it in the next chapters. Next: Java Polymorphism.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Build a BankAccount class
Calculate areas with inheritance and polymorphism
Use a sealed interface and pattern matching
Interview Questions & Answers
What is a class and an object in Java?
What is a constructor in Java?
What is the difference between a constructor and a method in Java?
What is the this keyword in Java?
What is the static keyword in Java?
What is the final keyword in Java?
What are the four pillars of OOP in Java?