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

Java में Single Inheritance

Single inheritance तब है जब एक class का बिल्कुल एक parent होता है, एक biological parent से copy करने वाले एक child जैसा। यह parent का code reuse करता है और अपना खुद का जोड़ता है।
Syntax
java
class Parent { }
class Child extends Parent { }

What is Single Inheritance?

Single inheritance का मतलब है एक class बिल्कुल एक direct parent extend करती है — Java जानबूझकर class inheritance को एक single parent तक restrict करता है (कुछ दूसरी languages के विपरीत) specifically multiple class inheritance से बनने वाली ambiguity problems से बचने के लिए।

उदाहरण: What is Single Inheritance?

java
class Animal {}
class Dog extends Animal { // exactly one direct parent
}
public class Main {
	public static void main(String[] args) {
		System.out.println(new Dog() instanceof Animal);
	}
}

Code Reusability

एक subclass अपने आप इसके parent के define किए हर public और protected member को पाता है, उस code को फिर से type किए बिना, जो inheritance के साथ मिलने वाला main code-reuse benefit है हर related class में shared logic copy-paste करने की तुलना में।

उदाहरण: Code Reusability

java
class Animal {
	void eat() {
		System.out.println("Eating");
	}
}
class Dog extends Animal {
}
public class Main {
	public static void main(String[] args) {
		new Dog().eat(); // reused without retyping
	}
}

Adding New Methods

इसके inherit करने से आगे, एक subclass अपनी खुद की पूरी तरह नई fields और methods declare करने के लिए free है जिनके बारे में parent class कुछ नहीं जानती — inheritance सिर्फ parent से child की ओर नीचे flow करता है, कभी दूसरे तरीके से नहीं।

उदाहरण: Adding New Methods

java
class Animal {
	void eat() {
		System.out.println("Eating");
	}
}
class Dog extends Animal {
	void bark() {
		System.out.println("Bark"); // new method, parent knows nothing about it
	}
}
public class Main {
	public static void main(String[] args) {
		Dog dog = new Dog();
		dog.eat();
		dog.bark();
	}
}

Private Members

Parent के private members जानबूझकर subclass के लिए invisible हैं, भले ही subclass technically उन्हें memory में "रखती" हो — यह parent की encapsulation को संरक्षित रखता है, subclass को उस hidden state के साथ interact करने के लिए parent के public या protected methods से होकर जाने पर मजबूर करते हुए।

उदाहरण: Private Members

java
class Animal {
	private String secret = "hidden";
}
class Dog extends Animal {
	void reveal() {
		// System.out.println(secret); // would not compile: private to Animal
		System.out.println("Cannot access private parent field");
	}
}
public class Main {
	public static void main(String[] args) {
		new Dog().reveal();
	}
}

Reference Variables

एक subclass object को parent class के रूप में typed एक variable में store करना (Animal a = new Dog();) एक आम और उपयोगी pattern है जो polymorphism enable करता है — general Animal type के खिलाफ लिखा code किसी भी specific subclass के साथ सही से काम कर सकता है जो pass किया गया।

उदाहरण: Reference Variables

java
class Animal {
	void sound() {
		System.out.println("Some sound");
	}
}
class Dog extends Animal {
	@Override
	void sound() {
		System.out.println("Bark");
	}
}
public class Main {
	public static void main(String[] args) {
		Animal a = new Dog(); // subclass stored in parent-typed variable
		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. Subclass में parent की private fields को सीधे access करने की कोशिश करना, जो एक compile error है।
  2. एक parent reference बनाना और एक child-only method call करना, जो एक compile error है।
  3. extends भूल जाना, इसलिए subclass कुछ भी inherit नहीं करता।

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.