BCA / B.Tech 8 min read

Inheritance in Java in Hindi | जावा में इनहेरिटेंस हिंदी में

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: Java Notes in Hindi | जावा नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Inheritance in Java in Hindi | जावा में इनहेरिटेंस हिंदी में - General Topics
  • • अवलोकन: Quick Answer: Inheritance in Java का मतलब है कि एक class (child / subclass) extends keyword लगाकर दूसरी class (parent / superclass) के fields और methods...
  • • मुख्य अवधारणाएं: inheritance in java in hindi, inheritance in hindi, what is inheritance in hindi, जावा में इनहेरिटेंस, inheritance in java,
Quick Answer: Inheritance in Java का मतलब है कि एक class (child / subclass) extends keyword लगाकर दूसरी class (parent / superclass) के fields और methods अपने आप पा लेती है। इससे एक बार लिखा code बार-बार reuse होता है। Java में classes के साथ single, multilevel और hierarchical inheritance होती है; multiple और hybrid inheritance सिर्फ interfaces की मदद से मिलती है।

Inheritance Meaning in Hindi

English शब्द inheritance का सीधा मतलब है विरासत: जो चीज़ माता-पिता से बच्चों को अपने आप मिलती है। जैसे बच्चे को पिता का घर और सरनेम मिलता है, वैसे ही Java में child class को parent class के variables और methods मिल जाते हैं।

Java (OOP) में inheritance का मतलब है: एक class का दूसरी class की properties और behaviour को अपनाना। जिस class से चीज़ें ली जाती हैं वह superclass / parent / base class है, और जो लेती है वह subclass / child / derived class है।

Inheritance OOP के चार मुख्य pillars में से एक है। बाकी pillars यहाँ पढ़ें: OOPs Concepts in Java in Hindi.

Advertisement

IS-A Relationship

Inheritance हमेशा IS-A relation दिखाती है। "Dog IS-A Animal" सही है, इसलिए class Dog extends Animal सही design है। "Car IS-A Engine" गलत है, क्योंकि car के अंदर engine होता है (HAS-A)। HAS-A के लिए inheritance नहीं, object को field बनाकर रखा जाता है।

Inheritance in Java: Syntax

class Parent {
    // fields और methods
}

class Child extends Parent {
    // Parent के non-private members यहाँ अपने आप मिलते हैं
    // Child अपने नए fields/methods भी जोड़ सकती है
}
  • extends keyword class से class की inheritance के लिए है।
  • implements keyword class से interface के लिए है।
  • Java में हर class अपने आप java.lang.Object class को extend करती है, भले आप extends न लिखें। इसीलिए हर object में toString(), equals() जैसे methods पहले से होते हैं।

Inheritance in Java Example (Single Inheritance)

सबसे आसान inheritance program: Programmer class, Employee class से salary field पाती है।

class Employee {
    int salary = 40000;
}

class Programmer extends Employee {
    int bonus = 10000;
}

public class Main {
    public static void main(String[] args) {
        Programmer p = new Programmer();
        System.out.println("Salary: " + p.salary);   // Employee से मिला
        System.out.println("Bonus: " + p.bonus);
        System.out.println("Total: " + (p.salary + p.bonus));
    }
}

Output:

Salary: 40000
Bonus: 10000
Total: 50000

Programmer class में salary लिखा ही नहीं गया, फिर भी p.salary चलता है। यही inheritance है।

Types of Inheritance in Java

TypeStructureJava में classes से?
SingleA → Bहाँ
MultilevelA → B → Cहाँ
HierarchicalA → B, A → Cहाँ
MultipleA, B → Cनहीं (सिर्फ interfaces से)
Hybridऊपर वालों का mixMultiple वाला हिस्सा हो तो सिर्फ interfaces से
Single        Multilevel      Hierarchical       Multiple (interface से)
  A              A                A               I1     I2
  |              |              /   \               \   /
  B              B             B     C                C
                 |
                 C

1. Single Inheritance

एक child class, एक parent class से inherit करती है। ऊपर वाला Employee → Programmer example single inheritance है।

2. Multilevel Inheritance in Java

जब एक class किसी ऐसी class से inherit करती है जो खुद किसी और की child है, तो chain बनती है: Animal → Dog → Puppy। Puppy को Dog और Animal दोनों के methods मिलते हैं। Constructors हमेशा ऊपर से नीचे (parent पहले) चलते हैं, क्योंकि हर constructor की पहली line में compiler अपने आप super() जोड़ देता है।

class Animal {
    Animal() { System.out.println("Animal constructor"); }
    void eat() { System.out.println("Animal is eating"); }
}

class Dog extends Animal {
    Dog() { System.out.println("Dog constructor"); }
    void bark() { System.out.println("Dog is barking"); }
}

class Puppy extends Dog {
    Puppy() { System.out.println("Puppy constructor"); }
    void weep() { System.out.println("Puppy is weeping"); }
}

public class Main {
    public static void main(String[] args) {
        Puppy p = new Puppy();
        p.eat();    // Animal से
        p.bark();   // Dog से
        p.weep();   // अपना
    }
}

Output:

Animal constructor
Dog constructor
Puppy constructor
Animal is eating
Dog is barking
Puppy is weeping

3. Hierarchical Inheritance

एक parent से कई child classes बनती हैं। Dog और Cat दोनों Animal से inherit करती हैं, पर Dog को Cat के methods नहीं मिलते।

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..."); }
}

public class Main {
    public static void main(String[] args) {
        Cat c = new Cat();
        c.meow();
        c.eat();
        // c.bark();  // Compile error: Cat में bark() नहीं है
    }
}

Output:

Meowing...
Eating...

4. Multiple Inheritance in Java

एक class का दो या ज़्यादा classes से inherit करना Java में allowed नहीं है। class C extends A, B लिखने पर compile error आता है। पर एक class कई interfaces implement कर सकती है, और एक interface कई interfaces को extend कर सकता है। इसी तरीके से Java में multiple inheritance मिलती है।

interface Printable {
    void print();
}
interface Showable {
    void show();
}

class Document implements Printable, Showable {
    public void print() { System.out.println("Printing document"); }
    public void show()  { System.out.println("Showing document"); }
}

public class Main {
    public static void main(String[] args) {
        Document d = new Document();
        d.print();
        d.show();
    }
}

Output:

Printing document
Showing document

Interface क्या होता है और abstract class से कैसे अलग है, यह Abstraction in Hindi note में देखें।

5. Hybrid Inheritance in Java

Hybrid inheritance दो या ज़्यादा types का mix है। अगर mix सिर्फ single, multilevel और hierarchical का है, तो यह classes से भी बन जाता है। पर अगर उसमें multiple inheritance वाला हिस्सा है, तो Java में वह हिस्सा interface से ही बनेगा। नीचे Bird और Dog, Animal से inherit करती हैं (hierarchical), और Bird साथ में Flyable interface भी implement करती है (multiple)।

interface Flyable {
    void fly();
}

class Animal {
    void eat() { System.out.println("Animal eats"); }
}

class Bird extends Animal implements Flyable {
    public void fly() { System.out.println("Bird flies"); }
}

class Dog extends Animal {
    void bark() { System.out.println("Dog barks"); }
}

public class Main {
    public static void main(String[] args) {
        Bird b = new Bird();
        b.eat();
        b.fly();
        Dog d = new Dog();
        d.eat();
        d.bark();
    }
}

Output:

Animal eats
Bird flies
Animal eats
Dog barks

Java में Multiple Inheritance क्यों नहीं है? (Diamond Problem)

        A   (show())
       / \
      B   C   (दोनों show() को override करते हैं)
       \ /
        D   → d.show() किसका चले? B का या C का?

अगर B और C दोनों A के show() को अपने तरीके से लिखें, और D दोनों को extend करे, तो compiler तय नहीं कर पाएगा कि d.show() पर कौन सा version चले। इसे diamond problem कहते हैं। Java के designers ने इस confusion से बचने के लिए classes में multiple inheritance हटा दी।

Interfaces में Java 8 से default methods आ गए। अगर दो interfaces में एक ही नाम का default method हो, तो implement करने वाली class को वह method खुद override करना ज़रूरी है, वरना compile error आता है। Class चाहे तो InterfaceName.super.methodName() (जैसे A.super.show()) लिखकर किसी एक interface का version call कर सकती है। यानी फ़ैसला programmer करता है, compiler को guess नहीं करना पड़ता।

Advertisement

super Keyword और Constructor Chaining

  • super.methodName(): parent class का method call करता है, खासकर जब child ने उसे override किया हो।
  • super.variable: parent का वह variable, जिसका नाम child के variable जैसा ही हो।
  • super(arguments): parent का constructor call करता है। यह constructor की पहली line में ही लिखा जा सकता है।

अगर parent में सिर्फ parameterized constructor है (no-argument constructor नहीं), तो child को super(...) खुद लिखना पड़ता है, वरना compile error आता है।

class Person {
    String name;
    Person(String name) { this.name = name; }
    void display() { System.out.println("Name: " + name); }
}

class Student extends Person {
    int roll;
    Student(String name, int roll) {
        super(name);          // Person का constructor
        this.roll = roll;
    }
    void display() {          // override
        super.display();      // पहले Person वाला display()
        System.out.println("Roll: " + roll);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s = new Student("Amit", 21);
        s.display();
    }
}

Output:

Name: Amit
Roll: 21

Child जब parent के method को उसी signature के साथ दोबारा लिखती है, तो उसे Method Overriding कहते हैं। यह inheritance के बिना possible नहीं है।

Child Class को क्या मिलता है और क्या नहीं?

MemberChild में मिलता है?
public और protected fields/methodsहाँ
default (बिना modifier) membersसिर्फ तब, जब child उसी package में हो
private fields/methodsसीधे access नहीं होते (public getter/setter से इस्तेमाल कर सकते हैं)
ConstructorsInherit नहीं होते, पर super() से call होते हैं
final methodमिलता है, पर override नहीं हो सकता
final class (जैसे String)इसे extend ही नहीं किया जा सकता

public, protected, default और private का पूरा फ़र्क: Access Modifiers in Hindi.

Inheritance के फायदे और कमियाँ

फायदेकमियाँ
Code reusability: common code एक बार parent में लिखेंParent और child आपस में कसकर जुड़ जाते हैं (tight coupling)
Method overriding और runtime polymorphism possible होते हैंParent में बदलाव से सारी child classes पर असर पड़ता है
Classes का logical hierarchy बनता है, code समझना आसानबहुत गहरी chain (कई levels) को पढ़ना और debug करना मुश्किल
Maintenance आसान: bug एक जगह ठीक करेंगलत IS-A relation बनाने से design खराब होता है

Common Mistakes

  • class C extends A, B लिखना: Java में compile error।
  • super() को constructor में पहली line के बाद लिखना: compile error।
  • Private field को child में सीधे obj.field से access करना।
  • सोचना कि child का constructor पहले चलता है: हमेशा parent का constructor पहले चलता है।
  • Cyclic inheritance (class A extends B और class B extends A): compile error।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Inheritance in Java क्या है? (एक class का extends से दूसरी class के members पाना)
  2. Java किन types की inheritance को classes से support करता है? (Single, multilevel, hierarchical)
  3. super keyword के तीन use लिखिए। (parent method, parent variable, parent constructor)
  4. Diamond problem क्या है?
  5. Multilevel inheritance में constructors किस order में चलते हैं? (Parent से child की ओर)

Long Questions (5–10 marks)

  1. Inheritance क्या है? Java में इसके types diagram और example program के साथ समझाइए।
  2. Java multiple inheritance को classes से support क्यों नहीं करता? Interfaces से multiple inheritance कैसे मिलती है? Program दीजिए।
  3. Multilevel inheritance का program लिखिए और constructor calling order output के साथ समझाइए।
  4. super keyword और method overriding को एक program से explain कीजिए।

FAQs

Java में कितने types की inheritance होती है?

Theory में पाँच: single, multilevel, hierarchical, multiple और hybrid। Classes से सिर्फ पहले तीन बनते हैं। Multiple (और उसे शामिल करने वाली hybrid) inheritance interfaces से बनती है।

क्या Java में hybrid inheritance possible है?

हाँ, पर एक शर्त के साथ। अगर hybrid में किसी class के दो parents बनते हैं, तो उनमें से सिर्फ एक class हो सकती है, बाकी interfaces होने चाहिए। जैसे class Bird extends Animal implements Flyable।

क्या constructor inherit होता है?

नहीं। Constructor का नाम class के नाम जैसा होता है, इसलिए वह child में inherit नहीं होता। Child का constructor super() से parent के constructor को call करता है।

क्या private members inherit होते हैं?

Private field child object के अंदर memory में रहता है, पर child class उसे सीधे नाम से access नहीं कर सकती। Exam में लिखें: "private members child class में accessible नहीं होते।"

Inheritance और method overloading में क्या संबंध है?

Overloading के लिए inheritance ज़रूरी नहीं है, वह एक ही class में हो सकता है। Overriding के लिए inheritance ज़रूरी है, क्योंकि child, parent के method को दोबारा लिखती है।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Inheritance in Java का मतलब है कि एक class (child / subclass) extends keyword लगाकर दूसरी class (parent / superclass) के fields और methods अपने आप पा लेती है।
Java Notes in Hindi | जावा नोट्स हिंदी में के पाठ्यक्रम में Inheritance in Java in Hindi | जावा में इनहेरिटेंस हिंदी में एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Inheritance in Java in Hindi | जावा में इनहेरिटेंस हिंदी में के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement

In this Chapter

Inheritance in Java in Hindi | जावा में इनहेरिटेंस हिंदी में
BCA || Java 2024 Paper || MDSU Exam Paper
What is Java, Characteristics of Java - Java क्या है और जावा की विशेषताएं
Difference between Java and C++ in Hindi. (Java और C++ में अंतर )
Applications of Java Programming in Hindi
OOPS concepts in Java Hindi
What is JVM and JRE in hindi
Features of JAVA in hindi
What is Java bean in hindi & advantage in Hindi
Difference between Java and JavaScript in Hindi
JIT Compiler in Hindi
What is Control Statement (structure) in Hindi
What is variable in hindi (वेरिएबल क्या होता है?):-
Java URL Class in Hindi
What is Java swing and its methods in Hindi
Operators in Java in Hindi
Class & Method in Hindi
instance Operator in Hindi
Token in Hindi
Data Types in Hindi
Constructor in Hindi
Object-Oriented-Design in Hindi
Conditional Operator in Hindi
Constant in Hindi
Reference Variable in Hindi
Type Casting in Hindi
Literals in Hindi
Identifiers in Hindi
Array in Hindi
Anonymous Array in Hindi
Access Modifiers in Hindi
Enum Data type in Hindi
Keywords in Hindi-Java
Super Keywords in Hindi
Static Keywords in Hindi
Final Keywords in Hindi
New Operator in Hindi
Method Types in Hindi
Method Overloading in Hindi
Method Overriding in Hindi
Method Hiding in Hindi
Class & Object in Hindi
Diffrence Beetween Class & Object in Hindi
Math Class in Hindi
Abstraction in Hindi
Adapter Pattern in Hindi
Package in Hindi
Class & Structure in Java in Hindi
Exception Handling in Hindi