← Back to Core Java Course | Chapter 8: Inheritance | Lesson 4 of 7

Java Hierarchical Inheritance

What is Hierarchical Inheritance?

Hierarchical inheritance is the mirror image of multilevel inheritance: instead of a chain, multiple subclasses branch off from one shared parent class, each inheriting the same base functionality independently.

Example: What is Hierarchical Inheritance?

java
class Animal {
	void eat() { System.out.println("Eating"); }
}
class Dog extends Animal {}
class Cat extends Animal {} // two siblings from one parent
public class Main {
	public static void main(String[] args) {
		new Dog().eat();
		new Cat().eat();
	}
}

Independent Subclass States

Each branch of the hierarchy is fully independent from its siblings — modifying or overriding behavior in one subclass has zero effect on the others, even though they share a common ancestor.

Example: Independent Subclass States

java
class Animal {
	void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
	@Override
	void sound() { System.out.println("Bark"); }
}
class Cat extends Animal {
}
public class Main {
	public static void main(String[] args) {
		new Dog().sound();
		new Cat().sound(); // unaffected by Dog's override
	}
}

Type Casting and Sibling Classes

Because sibling subclasses aren't related to each other (only to their shared parent), you can't cast an object of one sibling directly into another sibling type — that cast would fail at runtime with a ClassCastException.

Example: Type Casting and Sibling Classes

java
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
public class Main {
	public static void main(String[] args) {
		Animal a = new Dog();
		// Cat c = (Cat) a; // would throw ClassCastException at runtime
		System.out.println("Cannot cast Dog to Cat");
	}
}

Shared Static Members in Hierarchy

Static members declared on the shared parent class are genuinely shared across every subclass in the hierarchy, since static state belongs to the class itself rather than to any particular branch.

Example: Shared Static Members in Hierarchy

java
class Animal {
	static int count = 0;
}
class Dog extends Animal {}
class Cat extends Animal {}
public class Main {
	public static void main(String[] args) {
		Dog.count++;
		Cat.count++;
		System.out.println(Animal.count); // shared: 2
	}
}

Polymorphic Hierarchy Handling

A very common and useful pattern is storing objects from several different sibling subclasses in one array or collection typed as the shared parent, then looping over them and calling shared (often overridden) methods polymorphically.

Example: Polymorphic Hierarchy Handling

java
class Animal {
	void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
	@Override
	void sound() { System.out.println("Bark"); }
}
class Cat extends Animal {
	@Override
	void sound() { System.out.println("Meow"); }
}
public class Main {
	public static void main(String[] args) {
		Animal[] animals = { new Dog(), new Cat() };
		for (Animal a : animals) {
			a.sound();
		}
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.