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

Java में Multilevel Inheritance

Multilevel inheritance parents की एक chain है, grandparent, parent, और child जैसा, जहां हर level चीज़ें नीचे पास करता है। सबसे youngest सभी से features पाता है।
Syntax
java
class Grandparent { }
class Parent extends Grandparent { }
class Child extends Parent { }

What is Multilevel Inheritance?

Multilevel inheritance classes को chain करता है — C, B extend करता है, और B, A extend करता है — इसलिए C अपने direct parent B और, transitively, B के अपने parent A दोनों से inherit करता है, एक multi-tier hierarchy बनाते हुए।

उदाहरण: 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

Chain में कोई भी class इसके inherited किसी method को override कर सकती है, और वह override इस और chain में इसके नीचे हर चीज़ पर apply होता है, हर tier को hierarchy में नीचे जाते हुए progressively behavior customize करने देते हुए।

उदाहरण: 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 topmost ancestor से असल में instantiated की जा रही class तक order में चलते हैं — A का constructor पहले चलता है, फिर B का, फिर C का — सुनिश्चित करते हुए कि hierarchy का हर level अगला level शुरू होने से पहले object की state का अपना हिस्सा initialize कर सके।

उदाहरण: Constructor Chaining

java
// Define the class A
class A {
	A() { System.out.println("A constructor"); }
}
// Define the class B
class B extends A {
	B() { System.out.println("B constructor"); }
}
class C extends B {
	C() { System.out.println("C constructor"); }
}
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		new C();
	}
}

The super Keyword in Chains

Class C के अंदर super सिर्फ इसके immediate parent B तक पहुंचता है, grandparent A तक सीधे नहीं — अगर C को specifically A से कुछ चाहिए, इसे B से होकर जाना चाहिए (मान लें B इसे expose करता है), क्योंकि super levels skip नहीं करता।

उदाहरण: 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

Chain में सबसे नीचे वाली class से बना एक object इसके ऊपर की हर class का instance माना जाता है, इसलिए A, B, या C के खिलाफ instanceof checks सभी उस single object के लिए true return करेंगे।

उदाहरण: Instanceof in Multilevel Chains

java
// Define the class A
class A {}
// Define the class B
class B extends A {}
class C extends B {}
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new C object and store it in c
		C c = new C();
		// Print a line to the console
		System.out.println(c instanceof A);
		// Print a line to the console
		System.out.println(c instanceof B);
		System.out.println(c instanceof C);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि constructors top parent से child तक चलते हैं।
  2. यह भूल जाना कि super.method() की एक call सिर्फ direct parent version चलाती है, इसलिए grandparent behaviour skip हो जाता है जब तक parent इसे call न करे।
  3. Chains इतनी deep बनाना कि base class में एक change कई subclasses तोड़ दे।

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.