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

Java Multilevel Inheritance

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?

java
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);
	}
}

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

java
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
	}
}

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

java
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();
	}
}

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

java
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();
	}
}

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

java
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 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.