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

Java में Hierarchical Inheritance

Hierarchical inheritance तब है जब एक parent के कई children हों, उसी mom से inherit करने वाले siblings जैसा। हर child फिर अपने तरीके से अलग हो सकता है।
Syntax
java
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?

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

Independent Subclass States

Hierarchy की हर branch इसके siblings से पूरी तरह independent है — एक subclass में behavior modify या override करना दूसरों पर zero effect डालता है, भले ही वे एक common ancestor share करते हों।

उदाहरण: Independent Subclass States

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

Type Casting and Sibling Classes

चूंकि sibling subclasses एक-दूसरे से related नहीं हैं (सिर्फ उनके shared parent से), आप एक sibling के object को सीधे दूसरे sibling type में cast नहीं कर सकते — वह cast runtime पर एक ClassCastException के साथ fail होगी।

उदाहरण: Type Casting and Sibling Classes

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

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

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

Polymorphic Hierarchy Handling

एक बहुत आम और उपयोगी pattern shared parent से typed एक array या collection में कई अलग sibling subclasses से objects store करना है, फिर उन पर loop करना और shared (अक्सर overridden) methods polymorphically call करना।

उदाहरण: Polymorphic Hierarchy Handling

java
// 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();
		}
	}
}
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. एक sibling class को दूसरे में cast करना, जैसे Dog को Cat में, जो ClassCastException throw करता है।
  2. हर child class में वही code रखना, जब shared behaviour parent में belong करता है।
  3. यह उम्मीद करना कि एक child class के object में एक change दूसरे के objects को प्रभावित करेगा, जब हर object अपनी state रखता है।

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.