Java Inheritance In Hindi

📔 : Java 🔗

OOPs concepts में यह बहुत ही important topic है।

किसी class को या Class की properties / behavior को extend करना Inheritance कहते हैं। जिस class को extend किया गया है उसे Parent / Base Class , जिस class के द्वारा extend किया गया है उसे Child / Derived Class कहते हैं। Extend करने पर Child Class में लगभग बो सभी properties / methods होंगे जो Parent Class में हैं, हालाँकि यह Parent Class के Access Modifiers पर depend करता है कि बो Properties Accessible हैं या नहीं।

किसी class को inherit करने के लिए extends keyword का use किया जाता है।

class A {
String brand = "Ford"; void methodA() { System.out.println("method of class A"); } } class B extends A { public static void main(String[] args) { B b = new B(); b.methodA(); } }

example में आप देख सकते हैं , कि कैसे B class में class A का method access किया। इसी तरह से जो भी properties / methods (private members को छोड़कर ) parent class में मौजूद होती हैं उन सभी को इसके child class द्वारा access किया जा सकता है।

child class में parent class की private properties और method को access नहीं किया जा सकता है।

Constructor In Inheritance

जैसा कि आपने पढ़ा है कि जब कोई class किसी दूसरी class को Inherit करती है तो private properties और methods को छोड़कर बाकी सब भी इसके child class में Inherit हो जाता है। Inheritance में parent class के constructor automatically inherit हो जाते हैं। तो जब भी आप child class का object बनाएंगे तो दोनों classes को constructors automatically run होंगे।

हालाँकि parent class के constructors पहले run होंगे फिर उसके बाद child class के।
See Example -

class A {
// define constructor. A() { System.out.println("Class A constructor."); } } public class B extends A { // define constructor. B() { System.out.println("Class B constructor."); } public static void main(String[] args) { B b = new B(); } }

ध्यान रहे extend की जाने वाली class में define किये गए constructors private नहीं होने चाहिए , otherwise आप उस class को inherit भी नहीं कर पाओगे।

class A {
// define a properties. String message = "Hello"; } public class B extends A { void test() { // here access via super. System.out.println(super.message); } public static void main(String[] args) { B b = new B(); b.test(); } }

super को आप किसी static method में call नहीं कर सकते हैं , जैसा कि आपको पता होगा कि static method का कोई objects instance नहीं होता है। इसलिए static method में आप this और super का use नहीं कर पाएंगे।

Java Types of Inheritance

मुख्य रूप से Inheritance 5 तरह की होती है।-

  1. Single Inheritance

  2. Multilevel Inheritance

  3. Hierarchical Inheritance

  4. Multiple Inheritance

  5. Hybrid Inheritance

नीचे दी गयी images को देखकर आप समझ सकते हैं।

Image could not load

Types Of Inheritance

जब कोई class एक से ज्यादा classes जो inherit करती है तो उसे multiple Inheritance कहते हैं। हालाँकि java Multiple और Hybrid Inheritance को support नहीं करती है।

Image could not load

Types Of Inheritance -2

Java Single Inheritance

जब कोई single class किसी single class को inherit करती है तो उसे Single Inheritance कहते हैं। अभी तक आपने जो भी inheritance के example देखे वो सभी single inheritance के थे।

Java Multilevel Inheritance

जब single class inheritance की chain बनती है मतलब एक class को दूसरी class और दूसरी को तीसरी class फिर तीसरी class को चौथी। इस तरह से एक chain बन जाती है तो उसे Multilevel Inheritance कहते है। जैसा कि आप ऊपर दिए गए image में देख सकते हैं।

class A {
A() { System.out.println("Class A constructor."); } public void methoda() { System.out.println("Class A methodb."); } } // define another class that inerits class A. class B extends A { B() { System.out.println("Class B constructor."); } public void methodb() { System.out.println("Class B methodb."); } } // main class that inherits class B. public class C extends B { C() { System.out.println("Class C constructor."); } public static void main(String[] args) { C c = new C(); // you can access all the method/property of parent and super parent class. c.methoda(); c.methodb(); } }

Java Hierarchy Inheritance

जब किसी single class को एक से ज्यादा classes Inherit करती हैं तो उसे Hierarchy Inheritance कहते हैं।

class Animal{
void eat() { System.out.println("An animal is eating..."); } } // class that inherits class Animal. class Dog extends Animal{ void bark(){ System.out.println("The dog is barking..."); } } // another class that inherits same class : Animal. class Cat extends Animal{ void meow() { System.out.println("The cat is meowing..."); } } // main class. public class Main{ public static void main(String args[]){ Cat c=new Cat(); c.eat(); c.meow(); // now create Dog Object. Dog d = new Dog(); d.eat(); d.bark(); } }

Why java does not support multiple inheritance ?

complexity और ambiguity को avoid करने के लिए java Multiple / Hybrid Inheritance को support नहीं करती है। हम , जानते हैं कि parent class की सभी non-private और non-static variables और methods को super keywords के though child class में access कर सकते हैं।

अब suppose कीजिये कि किसी एक class ने एक साथ दो classes को inherit कर लिया और उन parent classes में same method / variable define किये हुए हैं तो को super के through access करने पर एक ambiguity create होगी और compiler confuse होगा कि उसे किस parent class से member access करना है। इसीलिए java Multiple / Hybrid Inheritance को support नहीं करती है।

अब आप java में Inheritance के बारे में अच्छे से समझ गए होंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers