Java Inheritance परिचय
In this page:
class ParentClass {
// parent members
}
class ChildClass extends ParentClass {
// child members
}
What is Inheritance?
Inheritance एक class (subclass) को extends उपयोग करके किसी दूसरी (superclass) की fields और methods acquire करने देता है, इसलिए shared behavior सिर्फ parent में एक बार लिखना है और हर child class में अपने आप available है।
उदाहरण: What is Inheritance?
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
}
public class Main {
public static void main(String[] args) {
new Dog().eat(); // inherited from Animal
}
}
Login to try C/C++/Java/PHP code in the editor
The super Keyword
super keyword एक subclass को इसके immediate parent की fields, methods, या constructors तक explicitly पहुंचने देता है — तब उपयोगी जब subclass को parent के behavior को पूरी तरह replace करने के बजाय extend करना हो।
उदाहरण: The super Keyword
class Animal {
String type = "Animal";
}
class Dog extends Animal {
String type = "Dog";
void show() {
System.out.println(super.type); // reaches the parent's field
}
}
public class Main {
public static void main(String[] args) {
new Dog().show();
}
}
Login to try C/C++/Java/PHP code in the editor
Method Overriding
Overriding तब होता है जब एक subclass अपने parent से inherited एक method को फिर से define करता है, उस method को subclass के लिए specific एक नया implementation देते हुए जबकि parent ने declared किया वही method signature रखते हुए।
उदाहरण: Method Overriding
// Define the class Animal
class Animal {
void sound() {
// Print a line to the console
System.out.println("Some sound");
}
}
// Define the class Dog
class Dog extends Animal {
// Override the method inherited from the parent type
@Override
void sound() {
// Print a line to the console
System.out.println("Bark");
}
}
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
new Dog().sound();
}
}
Login to try C/C++/Java/PHP code in the editor
IS-A Relationship
Inheritance एक genuine "is-a" relationship model करने के लिए है — एक Dog असल में एक Animal है, एक Sedan असल में एक Vehicle है — और उन cases में inheritance तक पहुंचना जो इस pattern में fit नहीं होते आमतौर पर awkward, hard-to-maintain class hierarchies की ओर ले जाता है।
उदाहरण: IS-A Relationship
class Vehicle {}
class Sedan extends Vehicle {} // a Sedan IS-A Vehicle
public class Main {
public static void main(String[] args) {
Vehicle v = new Sedan();
System.out.println(v instanceof Vehicle);
}
}
Login to try C/C++/Java/PHP code in the editor
Constructor Call Sequence
जब एक subclass object constructed होता है, parent class का constructor हमेशा पहले चलता है (implicitly, या explicitly super(...) के ज़रिए), और सिर्फ वह खत्म होने के बाद subclass की अपनी constructor body execute होती है।
उदाहरण: Constructor Call Sequence
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog constructor");
}
}
public class Main {
public static void main(String[] args) {
new Dog(); // Animal constructor runs first
}
}
Login to try C/C++/Java/PHP code in the editor
- जब parent का कोई default constructor न हो तो
super(...)call न करना, जो एक compile error है। - Inheritance उपयोग करना जहां classes का "is-a" relationship नहीं।
extendsसे एक से ज़्यादा class से inherit करने की कोशिश करना, जो Java allow नहीं करता।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: