Β 

πŸš€ Types of Inheritance in Java

Inheritance is one of the most powerful features of OOP in Java. It allows one class to acquire properties and behaviors (fields & methods) of another class. But did you know there are different types of inheritance?

πŸ“Œ Types of Inheritance in Java

Types of inheritance in java:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Multiple Inheritance
  5. Hybrid Inheritance

⚠️ Note: Java does not support multiple inheritance with classes (to avoid ambiguity) but it supports it through interfaces.

Generated image

1️⃣ Single Inheritance

  • One class inherits from another.

  • Example: A Dog class inherits from Animal.

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

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

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();   // Inherited method
        d.bark();  // Dog’s own method
    }
}

Output:

This animal eats food.
The dog barks.

2️⃣ Multilevel Inheritance

  • A chain of inheritance (like a family tree).

  • Example: Animal β†’ Dog β†’ Puppy

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

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

class Puppy extends Dog {
    void weep() {
        System.out.println("The puppy weeps.");
    }
}

public class Main {
    public static void main(String[] args) {
        Puppy p = new Puppy();
        p.eat();   // From Animal
        p.bark();  // From Dog
        p.weep();  // From Puppy
    }
}

Output:

This animal eats food.
The dog barks.
The puppy weeps.

3️⃣ Hierarchical Inheritance

  • Multiple classes inherit from a single parent.

  • Example: Cat and Dog both inherit from Animal.

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

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

class Cat extends Animal {
    void meow() {
        System.out.println("The cat meows.");
    }
}

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

        Cat c = new Cat();
        c.eat();
        c.meow();
    }
}

Output:

This animal eats food.
The dog barks.
This animal eats food.
The cat meows.

4️⃣ Multiple Inheritance (Through Interfaces)

  • A class can inherit from multiple interfaces.

  • Used when you need multiple behaviors without conflicts.

interface Engine {
    void start();
}

interface MusicSystem {
    void playMusic();
}

class Car implements Engine, MusicSystem {
    public void start() {
        System.out.println("Car engine starts.");
    }
    public void playMusic() {
        System.out.println("Playing music in the car.");
    }
}

public class Main {
    public static void main(String[] args) {
        Car c = new Car();
        c.start();
        c.playMusic();
    }
}

Output:

Car engine starts.
Playing music in the car.

🎯 Quick Recap:

βœ… Single β†’ One parent β†’ one child
βœ… Multilevel β†’ Grandparent β†’ parent β†’ child
βœ… Hierarchical β†’ One parent β†’ many children
βœ… Multiple (via interfaces) β†’ Many parents β†’ one child

πŸ’‘ Real-world Analogy:
Think of inheritance like a family tree:

  • Your grandparents pass traits β†’ parents β†’ you.

  • But you may also learn behaviors from different teachers (interfaces).

πŸ‘‰ Next Step: Use inheritance wisely β€” it’s the backbone of clean, reusable, and scalable code in Java!

Β