Hierarchical Inheritance in Java

Hierarchical inheritance is an object-oriented pattern where a single parent class is extended by multiple child classes. Each child inherits the same base functionality but can also add or override its own specific behavior.

How Hierarchical Inheritance Works

Imagine a base class called Vehicle with common properties like speed and fuelType. Both a Car class and a Bike class could extend Vehicle independently, each inheriting the shared properties while adding their own unique methods.

  • Promotes code reuse by centralizing shared logic in the parent class
  • Each subclass remains independent and can override parent methods as needed
  • Common in real-world modeling — different types of a general category sharing a base definition

Hierarchical vs Other Inheritance Types

Unlike single inheritance (one parent, one child) or multilevel inheritance (a chain of classes), hierarchical inheritance is defined by one parent branching out to several children at the same level.

Hierarchical inheritance is one of the clearest illustrations of the "is-a" relationship in OOP — a Car is a Vehicle, and so is a Bike.

Master Java with Uncodemy

Hands-on training, live projects, and placement support in our Java Programming Course.

Explore the Course

Writing the Vehicle Example in Java

Here is the Vehicle, Car and Bike idea as running code. The parent is abstract because a plain "vehicle" is never created on its own, and each child supplies its own wheel count plus one method nobody else has.

abstract class Vehicle {
    protected String model;
    protected int speed;

    Vehicle(String model) {
        this.model = model;
    }

    void accelerate(int amount) {
        speed += amount;
        System.out.println(model + " is now at " + speed + " km/h");
    }

    abstract int wheels();
}

class Car extends Vehicle {
    Car(String model) { super(model); }

    @Override
    int wheels() { return 4; }

    void openBoot() { System.out.println(model + ": boot opened"); }
}

class Bike extends Vehicle {
    Bike(String model) { super(model); }

    @Override
    int wheels() { return 2; }

    void kickStart() { System.out.println(model + ": engine started"); }
}

public class Garage {
    public static void main(String[] args) {
        Vehicle[] fleet = { new Car("Hatchback"), new Bike("Cruiser") };
        for (Vehicle v : fleet) {
            v.accelerate(20);
            System.out.println("Wheels: " + v.wheels());
            if (v instanceof Car car) {
                car.openBoot();
            }
        }
    }
}

The loop treats every object as a Vehicle, yet wheels() returns 4 or 2 depending on the real object. The instanceof check with a pattern variable is how you safely reach a child-only method.

What Each Child Can and Cannot See

  • public and protected members of Vehicle are available inside both Car and Bike.
  • private members stay inside Vehicle. Children reach them only through public or protected methods.
  • Constructors are not inherited. Each child writes its own and calls super(...), which runs the parent constructor first.
  • Siblings know nothing about each other. Car cannot call kickStart(), because that method lives only in Bike.

Mistakes That Show Up in Hierarchies

Casting between siblings

Vehicle v = new Bike("Cruiser");
Car c = (Car) v;   // compiles, then throws ClassCastException at runtime

The compiler accepts the cast because a Vehicle might be a Car. At runtime the JVM checks the real object and refuses. Use instanceof before casting.

Other common problems

  • Moving child-specific logic, such as boot handling, into the parent. Every sibling then inherits behaviour that makes no sense for it.
  • Building deep, wide trees just to share one helper method. A shared utility class or composition is often cleaner.
  • Forgetting @Override, so a typo in the method name creates a new method instead of replacing the parent's version.

Choosing Between a Parent Class and an Interface

Use hierarchical inheritance when the children genuinely share state and code, such as a speed field and an accelerate method. If they only share a capability, like being chargeable or printable, an interface is the better tool, because a Java class can implement many interfaces but extend only one class.

Quick checklist before you design a hierarchy:

  • Does every child pass the "is-a" test with the parent?
  • Is the shared code truly common to all children?
  • Should the parent be abstract because it is never used directly?
  • Would an interface or composition give the same reuse with less coupling?