Java Hierarchical Inheritance
In this page:
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?
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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");
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: