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

Inheritance in Java: Types and Examples (extends, super)

Inheritance lets a child class reuse parent code using extends. See single, multilevel and hierarchical types, super keyword, and why multiple inheritance is banned.

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

What is inheritance in Java?

Inheritance is the mechanism where a child class acquires the fields and methods of a parent class using extends, enabling code reuse and an is-a relationship.

Why does Java not support multiple inheritance with classes?

Because of the diamond problem: if two parents define the same method, the child inherits an ambiguous version. Java offers multiple inheritance through interfaces instead.

What are the uses of the super keyword?

super() calls the parent constructor as the first statement, super.method() calls the parent version of an overridden method, and super.field accesses a parent field hidden by the child.