🌳
OOP

Inheritance

Parent → Child
💡 Inheritance में child class अपने parent class के features "विरसे" (inherit) में पाती है — जैसे बेटा अपने पापा की properties पाता है, plus ख़ुद की नई चीज़ें add कर सकता है।

extends keyword से एक class दूसरी class के fields और methods use कर सकती है, बिना दोबारा लिखे।

ये code reuse बढ़ाता है — common चीज़ें parent में लिखो, specific चीज़ें child में।

class Animal {
  void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal {
  void bark() { System.out.println("Woof!"); }
}
🌳
Inheritance में child class अपने parent class के features "विरसे" (inherit) में पाती है — जैसे बेटा अपने पापा की properties पाता है, plus ख़ुद की नई चीज़ें add कर सकता है।
1 / 4
⚡ झट से Recap
  • extends से parent के features मिलते हैं
  • Code reuse बढ़ता है
  • Child अपनी नई चीज़ें भी add करता है
इस page में (4 subtopics)

Single: एक class एक ही parent extend करती है (Dog extends Animal) — सबसे common type।

Multilevel: chain बनती है (Puppy extends Dog extends Animal) — Puppy को Dog के भी features मिलते हैं और Animal के भी (transitively)।

Hierarchical: एक parent के multiple children होते हैं (Dog और Cat दोनों Animal extend करते हैं) — दोनों को Animal का common behavior मिलता है, लेकिन अपना-अपना specific behavior भी।

Multiple inheritance (एक class दो classes extend करे, जैसे class C extends A, B) Java classes के लिए allowed नहीं है — "diamond problem" से बचने के लिए (अगर A और B दोनों में same-नाम का method हो, तो C को पता नहीं चलेगा कौनसा use करे)। ये interfaces के through partially achieve होता है (एक class multiple interfaces implement कर सकती है)।

TypeExample
SingleDog extends Animal
MultilevelPuppy extends Dog extends Animal
HierarchicalDog extends Animal, Cat extends Animal
Multiple (classes)Java में allowed नहीं

super.method() से parent class का overridden method call कर सकते हो (child के अंदर से) — useful जब child parent का behavior *extend* करना चाहता है, replace नहीं। super.field से parent का field access कर सकते हो अगर child में same नाम का field हो (variable "shadowing" हो रही हो)। super(...) parent constructor call करता है।

class Dog extends Animal {
  void makeSound() {
    super.makeSound(); // pehle parent ka version chalao
    System.out.println("Woof!"); // fir apna add karo
  }
}

Overriding के लिए method signature (नाम + parameters, same order/types) exactly same होना चाहिए parent और child में। Return type same या "covariant" (subtype) हो सकता है।

@Override annotation लगाना best practice है — compiler check कर लेता है कि तुम सच में override कर रहे हो (typo होने पर compile error मिलेगा, जो silent bug से कहीं better है)।

Access modifier child में parent से ज़्यादा restrictive नहीं हो सकता — public method को protected नहीं बना सकते overriding करते वक़्त (ये "visibility widen हो सकती है, narrow नहीं" वाला rule है)।

⚠️Common Mistake: @Override annotation के बिना, अगर तुम method नाम में typo कर दो (जैसे makeSound() लिखना भूल कर makeSond() लिख दो), Java उसे एक नया method समझेगा, error नहीं देगा — bug छुप जाएगा। @Override लगाने से compiler तुरंत पकड़ लेता है।

जब child class का object बनता है, सबसे पहले parent class का constructor चलता है (implicitly या super() से explicitly), फिर child का constructor। इससे guarantee मिलती है कि parent का setup पहले complete हो, child उसी के ऊपर build करे।

ये chain root तक जाती है — अगर 3-level inheritance है (A -> B -> C), तो object बनते वक़्त order होता है: A का constructor, फिर B का, फिर C का।