Inheritance क्या हैं? | what is Inheritance in Java in Hindi

इस आर्टिकल में हम जावा Inheritance क्या हैं? | what is Inheritance in Java in Hindi के बारे में जानेंगे।

what is Inheritance in Java in Hindi

Java में Inheritance एक mechanism है जिसमें एक object मूल object के सभी properties और behaviors को प्राप्त करती है। यह OOPs (ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग सिस्टम) का एक महत्वपूर्ण हिस्सा है।

जावा में inheritance के पीछे idea यह है कि आप new classes बना सकते हैं जो existing classes पर बनी हैं। जब आप किसी existing class से उत्तराधिकारी होते हैं, तो आप मूल class के methods और fields का reuse कर सकते हैं। इसके अलावा, आप अपनी current class में भी नए methods और fields जोड़ सकते हैं।

Why use inheritance in java | जावा में inheritance का उपयोग क्यों करें?

  • Method Overriding के लिए (इसलिए runtime polymorphism हासिल किया जा सकता है)।
  • Code पुन: Reusability के लिए।

Terms used in Inheritance

  • Class: एक class objects का एक समूह होता है जिसमें common properties होती हैं। यह एक टेम्प्लेट या ब्लूप्रिंट है जिससे ऑब्जेक्ट बनाए जाते हैं।
  • Sub Class/Child Class: Subclass एक ऐसा class है जो दूसरे class को inherits करता है। इसे एक व्युत्पन्न class, extended class या child class भी कहा जाता है।
  • Super Class/Parent Class: Superclass वह class है जहाँ से एक subclass सुविधाओं को प्राप्त करता है। इसे base क्लास या parent क्लास भी कहा जाता है।
  • Reusability: जैसा कि नाम specifies होता है, reusability एक mechanism है जो आपको एक new class बनाते समय मौजूदा class के fields और methods का reuse उपयोग करने की सुविधा प्रदान करता है। आप पिछली कक्षा में पहले से ही defined उन्हीं क्षेत्रों और methods का उपयोग कर सकते हैं।

The syntax of Java Inheritance in Hindi

        class Subclass-name extends Superclass-name  
        {  
           //methods and fields  
        }

        extends कीवर्ड indicate करता है कि आप एक new class बना रहे हैं जो existing class से प्राप्त होती है। “extends” का अर्थ functionality को increase है।

        जावा की terminology में, एक class जो विरासत में मिला है उसे parent या superclass कहा जाता है, और new class को child या subclass कहा जाता है।

        Java Inheritance Example in Hindi

        what is Inheritance in Java in Hindi

        जैसा कि ऊपर चित्र में दिखाया गया है, Programmer subclass है और Employee super class है। दो classes के बीच relation Programmer IS-A Employee है। इसका मतलब है कि Programmer एक प्रकार का Employee होता है।

          class Employee{  
           float salary=40000;  
          }  
          class Programmer extends Employee{  
           int bonus=10000;  
           public static void main(String args[]){  
             Programmer p=new Programmer();  
             System.out.println("Programmer salary is:"+p.salary);  
             System.out.println("Bonus of Programmer is:"+p.bonus);  
          }  
          } 

          Output:

           Programmer salary is:40000.0
           Bonus of programmer is:10000

          उपरोक्त उदाहरण में, प्रोग्रामर ऑब्जेक्ट स्वयं के class के साथ-साथ Employee class यानी कोड reusability के क्षेत्र तक पहुंच सकता है।

          Types of inheritance in java in Hindi

          class के आधार पर, जावा में तीन प्रकार की inheritance होती है: single, multilevel और hierarchical।

          जावा प्रोग्रामिंग में, multiple और hybrid inheritance केवल इंटरफ़ेस के माध्यम से supported है। हम बाद में इंटरफेस के बारे में जानेंगे।

          Types of inheritance in java in Hindi

          जावा में class के माध्यम से Multiple inheritance supported नहीं है।

          जब एक classes multiple classes को inherits करता है, तो इसे multiple inheritance के रूप में जाना जाता है। उदाहरण के लिए:

          Types of inheritance in java in Hindi

          Single Inheritance Example in Hindi

          जब एक क्लास दूसरी क्लास को इनहेरिट करती है तो इसे सिंगल इनहेरिटेंस कहते हैं। नीचे दिए गए उदाहरण में, Dog class Animal क्लास को inherits करता है, इसलिए सिंगल inheritance है।

          
          class Animal{  
          void eat(){System.out.println("eating...");}  
          }  
          class Dog extends Animal{  
          void bark(){System.out.println("barking...");}  
          }  
          class TestInheritance{  
          public static void main(String args[]){  
          Dog d=new Dog();  
          d.bark();  
          d.eat();  
          }}  

          Output:

          barking...
          eating...

          Multilevel Inheritance Example in Hindi

          जब inheritance की एक chain होती है, तो इसे multilevel inheritance के रूप में जाना जाता है। जैसा कि आप नीचे दिए गए उदाहरण में देख सकते हैं, BabyDog class Dog class inherits करती है जो फिर से Animal class इनहेरिट करती है, इसलिए एक multilevel inheritance है।

          class Animal{  
          void eat(){System.out.println("eating...");}  
          }  
          class Dog extends Animal{  
          void bark(){System.out.println("barking...");}  
          }  
          class BabyDog extends Dog{  
          void weep(){System.out.println("weeping...");}  
          }  
          class TestInheritance2{  
          public static void main(String args[]){  
          BabyDog d=new BabyDog();  
          d.weep();  
          d.bark();  
          d.eat();  
          }} 

          OutPut

          weeping...
          barking...
          eating...

          Hierarchical Inheritance Example in Hindi

          जब दो या दो से अधिक classes एक ही class को inherits करते हैं, तो इसे hierarchical inheritance के रूप में जाना जाता है। नीचे दिए गए उदाहरण में, Dog और Cat classes Animal class को inherits करते हैं, इसलिए hierarchical inheritance है।

          class Animal{  
          void eat(){System.out.println("eating...");}  
          }  
          class Dog extends Animal{  
          void bark(){System.out.println("barking...");}  
          }  
          class Cat extends Animal{  
          void meow(){System.out.println("meowing...");}  
          }  
          class TestInheritance3{  
          public static void main(String args[]){  
          Cat c=new Cat();  
          c.meow();  
          c.eat();  
          //c.bark();//C.T.Error  
          }}  

          Output:

          meowing...
          eating...

          Q) Why multiple inheritance is not supported in java? | जावा में multiple inheritance का supported क्यों नहीं किया जाता है?

          complexity को कम करने और language को सरल बनाने के लिए जावा में multiple inheritance का समर्थन नहीं किया जाता है।

          एक scenario पर विचार करें जहां A, B और C तीन classes हैं। C classe को A और B classes inherits में मिले हैं। यदि A और B classe में एक ही method है और आप इसे child class object से कॉल करते हैं, तो A या B classe की method को कॉल करने में ambiguity होगी।

          चूंकि compile-time errors runtime errors से बेहतर हैं, इसलिए यदि आप 2 क्लास इनहेरिट करते हैं तो जावा कंपाइल-टाइम errors देता है। तो चाहे आपके पास एक ही method हो या अलग, compile समय error होगी। Read More

          निष्कर्ष

          आशा करता हूँ कि आज के इस आर्टिकल में Inheritance क्या हैं? | what is Inheritance in Java in Hindi से सम्बंधित आपके सारे आंसर मिल गए होंगे।

          आज के इस आर्टिकल में हमने जाना कि Inheritance क्या हैं? | what is Inheritance in Java in Hindi इस आर्टिकल से संबधित आपका कोई प्रश्न हैं तो आप comment करें में आपकी कमेंट का जबाब जरूर दूंगा।

          ऐसे ही IT, Computer Science से रिलेटेड जानकारियाँ पाने के लिए हमारे इस वेबसाइट को सब्सक्राइब कर लीजिये | जिससे आपको हमारी आने वाले नये आर्किल की notification प्राप्त हो सके।

          अगर जानकारी अच्छी लगी हो तो अपने दोस्तों और class mate के साथ ये जानकारी जरूर शेयर करें जिससे उनकी भी हेल्प हो सके।

          FAQ प्रश्न-

            • Inheritance क्या हैं? | what is Inheritance in Java in Hindi

              Java में Inheritance एक mechanism है जिसमें एक object मूल object के सभी properties और behaviors को प्राप्त करती है। यह OOPs (ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग सिस्टम) का एक महत्वपूर्ण हिस्सा है।

            Related Posts

            Leave a Comment