Java में Hierarchical Inheritance
In this page:
class Parent { }
class ChildA extends Parent { }
class ChildB extends Parent { }
What is Hierarchical Inheritance?
Hierarchical inheritance multilevel inheritance की mirror image है: एक chain के बजाय, कई subclasses एक shared parent class से branch off होती हैं, हर एक independently वही base functionality inherit करते हुए।
उदाहरण: 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
Hierarchy की हर branch इसके siblings से पूरी तरह independent है — एक subclass में behavior modify या override करना दूसरों पर zero effect डालता है, भले ही वे एक common ancestor share करते हों।
उदाहरण: 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
चूंकि sibling subclasses एक-दूसरे से related नहीं हैं (सिर्फ उनके shared parent से), आप एक sibling के object को सीधे दूसरे sibling type में cast नहीं कर सकते — वह cast runtime पर एक ClassCastException के साथ fail होगी।
उदाहरण: 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
Shared parent class पर declared static members hierarchy के हर subclass में genuinely shared हैं, क्योंकि static state किसी particular branch के बजाय class खुद से belong करता है।
उदाहरण: 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
एक बहुत आम और उपयोगी pattern shared parent से typed एक array या collection में कई अलग sibling subclasses से objects store करना है, फिर उन पर loop करना और shared (अक्सर overridden) methods polymorphically call करना।
उदाहरण: Polymorphic Hierarchy Handling
// Define the class Animal
class Animal {
void sound() { System.out.println("Some sound"); }
}
// Define the class Dog
class Dog extends Animal {
// Override the method inherited from the parent type
@Override
void sound() { System.out.println("Bark"); }
}
class Cat extends Animal {
// Override the method inherited from the parent type
@Override
void sound() { System.out.println("Meow"); }
}
public class Main {
// Program entry point: the JVM starts running here
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
- एक sibling class को दूसरे में cast करना, जैसे
DogकोCatमें, जोClassCastExceptionthrow करता है। - हर child class में वही code रखना, जब shared behaviour parent में belong करता है।
- यह उम्मीद करना कि एक child class के object में एक change दूसरे के objects को प्रभावित करेगा, जब हर object अपनी state रखता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: