Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · Java · 04 Jul 2026 · Hindi + English

Java में Inheritance: Types और Examples (extends, super)

Inheritance से child class extends के ज़रिए parent का code reuse करती है. Single, multilevel, hierarchical types, super keyword, और multiple inheritance ban क्यों है.

What is inheritance?

Inheritance lets a child class acquire the fields and methods of a parent class using the extends keyword. The child reuses parent code and can add its own — the "is-a" relationship: a Dog is an Animal.
class Animal {                       // parent (superclass)
    void eat() { System.out.println("Eating..."); }
}

class Dog extends Animal {           // child (subclass)
    void bark() { System.out.println("Barking!"); }
}

Dog d = new Dog();
d.eat();     // inherited from Animal - no rewrite needed
d.bark();    // Dog's own method
Eating... Barking!

Types of inheritance in Java

// 1. SINGLE:        A -> B
class B extends A { }

// 2. MULTILEVEL:    A -> B -> C
class C extends B { }        // C gets A's + B's members

// 3. HIERARCHICAL:  A -> B, A -> C
class B extends A { }
class C extends A { }        // two children, one parent
Not supported with classes: multiple inheritance (class C extends A, B) — compile error. Reason: the diamond problem — if A and B both have show(), which one does C inherit? Java avoids the ambiguity; multiple inheritance is achieved through interfaces instead.

The super keyword

class Animal {
    String name = "Animal";
    Animal() { System.out.println("Animal constructor"); }
    void sound() { System.out.println("Some sound"); }
}

class Dog extends Animal {
    String name = "Dog";
    Dog() {
        super();                          // parent constructor (implicit anyway)
        System.out.println("Dog constructor");
    }
    void sound() {
        super.sound();                     // parent's version
        System.out.println("Bark! My parent field: " + super.name);
    }
}

new Dog().sound();
Animal constructor Dog constructor Some sound Bark! My parent field: Animal

Three uses of super: super() calls the parent constructor (always runs first), super.method() calls the parent's version, super.field reads the parent's hidden field.

Real-world example: school system

class Person {
    String name;
    Person(String n) { name = n; }
    void introduce() { System.out.println("I am " + name); }
}

class Teacher extends Person {
    String subject;
    Teacher(String n, String s) { super(n); subject = s; }
    void teach() { System.out.println(name + " teaches " + subject); }
}

class Student extends Person {
    int roll;
    Student(String n, int r) { super(n); roll = r; }
}

new Teacher("Gagan Sir", "Computer").teach();
new Student("Aman", 101).introduce();
Gagan Sir teaches Computer I am Aman

Person's code is written once; Teacher and Student both reuse it. This is the entire point: code reusability + logical hierarchy.

Inheritance क्या है?

Inheritance से child class extends keyword के ज़रिए parent class के fields और methods पा लेती है. Child parent का code reuse करता है और अपना जोड़ सकता है — "is-a" relationship: Dog एक Animal है.
class Animal {                       // parent (superclass)
    void eat() { System.out.println("Eating..."); }
}

class Dog extends Animal {           // child (subclass)
    void bark() { System.out.println("Barking!"); }
}

Dog d = new Dog();
d.eat();     // Animal से inherited - दोबारा लिखने की ज़रूरत नहीं
d.bark();    // Dog का अपना method
Eating... Barking!

Java में inheritance के types

// 1. SINGLE:        A -> B
class B extends A { }

// 2. MULTILEVEL:    A -> B -> C
class C extends B { }        // C को A के + B के members मिले

// 3. HIERARCHICAL:  A -> B, A -> C
class B extends A { }
class C extends A { }        // दो children, एक parent
Classes के साथ supported नहीं: multiple inheritance (class C extends A, B) — compile error. वजह: diamond problem — अगर A और B दोनों में show() हो, तो C किसका inherit करे? Java इस ambiguity से बचता है; multiple inheritance interfaces से achieve होती है.

super keyword

class Animal {
    String name = "Animal";
    Animal() { System.out.println("Animal constructor"); }
    void sound() { System.out.println("Some sound"); }
}

class Dog extends Animal {
    String name = "Dog";
    Dog() {
        super();                          // parent constructor (वैसे implicit है)
        System.out.println("Dog constructor");
    }
    void sound() {
        super.sound();                     // parent का version
        System.out.println("Bark! Parent field: " + super.name);
    }
}

new Dog().sound();
Animal constructor Dog constructor Some sound Bark! Parent field: Animal

super के तीन uses: super() parent constructor call करता है (हमेशा पहले चलता है), super.method() parent का version, super.field parent की hidden field.

Real-world example: school system

class Person {
    String name;
    Person(String n) { name = n; }
    void introduce() { System.out.println("I am " + name); }
}

class Teacher extends Person {
    String subject;
    Teacher(String n, String s) { super(n); subject = s; }
    void teach() { System.out.println(name + " teaches " + subject); }
}

class Student extends Person {
    int roll;
    Student(String n, int r) { super(n); roll = r; }
}

new Teacher("Gagan Sir", "Computer").teach();
new Student("Aman", 101).introduce();
Gagan Sir teaches Computer I am Aman

Person का code एक बार लिखा; Teacher और Student दोनों reuse करते हैं. यही पूरा point है: code reusability + logical hierarchy.

Frequently Asked Questions

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

Inheritance वह mechanism है जिसमें child class extends से parent class के fields और methods पा लेती है, जिससे code reuse और is-a relationship बनती है.

Java classes के साथ multiple inheritance support क्यों नहीं करता?

Diamond problem की वजह से: अगर दो parents same method define करें, तो child को ambiguous version मिलता है. Java की जगह interfaces से multiple inheritance देता है.

super keyword के uses क्या हैं?

super() पहले statement के रूप में parent constructor call करता है, super.method() overridden method का parent version call करता है, और super.field child द्वारा hide की गई parent field access करता है.