So far, you have seen inheritance moving from one class to another and then through multiple levels. Now consider a different situation: several child classes need the same parent class.
This structure is called hierarchical inheritance. One parent class is extended by multiple child classes, allowing each child to reuse common behavior while adding its own specialized functionality.
Hierarchical inheritance occurs when multiple child classes directly inherit from the same parent class.
The Basic Structure
Vehicle
/ \
↓ ↓
Car Bike
Here, both Car and Bike inherit from Vehicle. The parent contains common behavior, while each child can provide behavior specific to its own type.
A Simple Example
class Animal { void eat() { System.out.println("Animal is eating"); } } class Dog extends Animal { void bark() { System.out.println("Dog is barking"); } } class Cat extends Animal { void meow() { System.out.println("Cat is meowing"); } }
The Animal class contains the common eat() method. Both Dog and Cat inherit it, but each class also defines behavior specific to itself.
Using Different Child Classes
public class Main { public static void main(String[] args) { Dog dog = new Dog(); Cat cat = new Cat(); dog.eat(); dog.bark(); cat.eat(); cat.meow(); } }
Both objects can call eat() because that method comes from their common parent. However, the dog can call bark(), while the cat can call meow().
One parent → Multiple direct children is the easiest way to recognize hierarchical inheritance.
Why Is Hierarchical Inheritance Useful?
Hierarchical inheritance is useful when several related classes share a common foundation but have different specialized behavior. Instead of placing the same common code into every child class, the shared functionality can be centralized in the parent.
For example, an application may have several types of employees. Every employee may need a common work() method, while developers, designers, and testers perform different specialized tasks.
class Employee { void work() { System.out.println("Employee is working"); } } class Developer extends Employee { void writeCode() { System.out.println("Developer writes code"); } } class Designer extends Employee { void designInterface() { System.out.println("Designer creates interfaces"); } } class Tester extends Employee { void testApplication() { System.out.println("Tester tests the application"); } }
This design keeps the common employee behavior in one place while allowing each specialization to evolve independently.
Different Children Can Override the Same Method
One of the most useful aspects of hierarchical inheritance is that different child classes can provide different implementations of the same inherited method.
class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); } } class Cat extends Animal { @Override void sound() { System.out.println("Cat meows"); } }
The parent defines the general contract or behavior, while each child specializes it. This becomes especially powerful when child objects are handled through a parent reference, which leads directly into runtime polymorphism.
Parent Reference and Child Object
Animal dog = new Dog(); Animal cat = new Cat(); dog.sound(); cat.sound();
Even though both variables have the parent type Animal, Java selects the overridden method according to the actual object at runtime. The dog produces dog-specific behavior, while the cat produces cat-specific behavior.
Hierarchical inheritance often works closely with polymorphism: one common parent type can represent several specialized child types.
Hierarchical Inheritance Does Not Mean Multiple Inheritance
A common source of confusion is thinking that a child has multiple parents because several classes appear under one parent. That is not the case.
class Vehicle { } class Car extends Vehicle { } class Bike extends Vehicle { }
The Car class has one direct parent, and the Bike class also has one direct parent. They simply share the same parent.
| Inheritance Type | Structure | Main Idea |
|---|---|---|
| Single | A → B | One child extends one parent. |
| Multilevel | A → B → C | Inheritance continues through levels. |
| Hierarchical | A → B and A → C | Multiple children share one parent. |
Constructor Behavior
Constructors are not inherited, but when a child object is created, the parent constructor participates in initialization before the child constructor executes.
class Vehicle { Vehicle() { System.out.println("Vehicle constructor"); } } class Car extends Vehicle { Car() { System.out.println("Car constructor"); } } class Bike extends Vehicle { Bike() { System.out.println("Bike constructor"); } } public class Main { public static void main(String[] args) { new Car(); new Bike(); } }
When the Car object is created, the Vehicle constructor executes first, followed by the Car constructor. The same pattern occurs when the Bike object is created.
Common Beginner Mistakes
- Thinking hierarchical inheritance means one child has multiple parent classes.
- Confusing hierarchical inheritance with multilevel inheritance.
- Assuming a parent automatically knows about every child class.
- Forgetting that each child can provide its own implementation of an inherited method.
- Creating a large parent class containing unrelated behavior just because several classes exist below it.
Best Practice
Keep the parent class focused on behavior genuinely shared by all its children. If a method makes sense for only one child, it usually belongs in that child rather than in the parent.
This keeps the hierarchy meaningful and prevents the parent class from becoming a collection of unrelated features. A clean inheritance tree should make the domain easier to understand, not harder.
Interview Insight
A strong interview answer is: "Hierarchical inheritance occurs when multiple child classes directly extend the same parent class. It allows common behavior to be defined once in the parent while each child can add or override behavior specific to its own type."
Final Takeaway
Hierarchical inheritance gives several related classes a common foundation. A parent such as Animal can be extended by Dog, Cat, and other specialized classes, allowing shared behavior to remain centralized while each child develops its own identity. The next chapter moves deeper into the inheritance mechanism by examining the super keyword, which lets a child class explicitly interact with its parent class.
