Java में Multilevel Inheritance
In this page:
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?
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
Chain में कोई भी class इसके inherited किसी method को override कर सकती है, और वह override इस और chain में इसके नीचे हर चीज़ पर apply होता है, हर tier को hierarchy में नीचे जाते हुए progressively behavior customize करने देते हुए।
उदाहरण: 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 topmost ancestor से असल में instantiated की जा रही class तक order में चलते हैं — A का constructor पहले चलता है, फिर B का, फिर C का — सुनिश्चित करते हुए कि hierarchy का हर level अगला level शुरू होने से पहले object की state का अपना हिस्सा initialize कर सके।
उदाहरण: Constructor Chaining
// 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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
Chain में सबसे नीचे वाली class से बना एक object इसके ऊपर की हर class का instance माना जाता है, इसलिए A, B, या C के खिलाफ instanceof checks सभी उस single object के लिए true return करेंगे।
उदाहरण: Instanceof in Multilevel Chains
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि constructors top parent से child तक चलते हैं।
- यह भूल जाना कि
super.method()की एक call सिर्फ direct parent version चलाती है, इसलिए grandparent behaviour skip हो जाता है जब तक parent इसे call न करे। - Chains इतनी deep बनाना कि base class में एक change कई subclasses तोड़ दे।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: