Java Multilevel Inheritance
In this page:
What is Multilevel Inheritance?
Multilevel inheritance chains classes together — C extends B, and B extends A — so C ends up inheriting from both its direct parent B and, transitively, from B's own parent A, forming a multi-tier hierarchy.
Example: What is Multilevel Inheritance?
class A {}
class B extends A {}
class C extends B {} // chain: C -> B -> A
public class Main {
public static void main(String[] args) {
C c = new C();
System.out.println(c instanceof A);
}
}
Login to try C/C++/Java/PHP code in the editor
Method Overriding in Multilevel Chains
Any class in the chain can override a method it inherited, and that override applies to it and everything below it in the chain, letting each tier customize behavior progressively as you move down the hierarchy.
Example: Method Overriding in Multilevel Chains
class A {
void show() { System.out.println("A"); }
}
class B extends A {
@Override
void show() { System.out.println("B"); }
}
class C extends B {
}
public class Main {
public static void main(String[] args) {
new C().show(); // uses B's override
}
}
Login to try C/C++/Java/PHP code in the editor
Constructor Chaining
Constructors run in order from the topmost ancestor down to the class actually being instantiated — A's constructor runs first, then B's, then C's — ensuring every level of the hierarchy gets to initialize its own portion of the object's state before the next level begins.
Example: Constructor Chaining
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 Main {
public static void main(String[] args) {
new C();
}
}
Login to try C/C++/Java/PHP code in the editor
The super Keyword in Chains
super inside class C only reaches its immediate parent B, not grandparent A directly — if C needs something from A specifically, it has to go through B (assuming B exposes it), since super doesn't skip levels.
Example: The super Keyword in Chains
class A {
void show() { System.out.println("A"); }
}
class B extends A {
void showB() { super.show(); } // reaches only immediate parent A
}
class C extends B {
}
public class Main {
public static void main(String[] args) {
new C().showB();
}
}
Login to try C/C++/Java/PHP code in the editor
Instanceof in Multilevel Chains
An object created from the bottom class in the chain is considered an instance of every class above it, so instanceof checks against A, B, or C would all return true for that single object.
Example: Instanceof in Multilevel Chains
class A {}
class B extends A {}
class C extends B {}
public class Main {
public static void main(String[] args) {
C c = new C();
System.out.println(c instanceof A);
System.out.println(c instanceof B);
System.out.println(c instanceof C);
}
}
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: