Java में Single Inheritance
In this page:
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?
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);
}
}
Login to try C/C++/Java/PHP code in the editor
Code Reusability
एक subclass अपने आप इसके parent के define किए हर public और protected member को पाता है, उस code को फिर से type किए बिना, जो inheritance के साथ मिलने वाला main code-reuse benefit है हर related class में shared logic copy-paste करने की तुलना में।
उदाहरण: Code Reusability
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
}
}
Login to try C/C++/Java/PHP code in the editor
Adding New Methods
इसके inherit करने से आगे, एक subclass अपनी खुद की पूरी तरह नई fields और methods declare करने के लिए free है जिनके बारे में parent class कुछ नहीं जानती — inheritance सिर्फ parent से child की ओर नीचे flow करता है, कभी दूसरे तरीके से नहीं।
उदाहरण: Adding New Methods
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();
}
}
Login to try C/C++/Java/PHP code in the editor
Private Members
Parent के private members जानबूझकर subclass के लिए invisible हैं, भले ही subclass technically उन्हें memory में "रखती" हो — यह parent की encapsulation को संरक्षित रखता है, subclass को उस hidden state के साथ interact करने के लिए parent के public या protected methods से होकर जाने पर मजबूर करते हुए।
उदाहरण: Private Members
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();
}
}
Login to try C/C++/Java/PHP code in the editor
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
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();
}
}
Login to try C/C++/Java/PHP code in the editor
- Subclass में parent की
privatefields को सीधे access करने की कोशिश करना, जो एक compile error है। - एक parent reference बनाना और एक child-only method call करना, जो एक compile error है।
extendsभूल जाना, इसलिए subclass कुछ भी inherit नहीं करता।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: