Launch your tech mastery with us—your coding journey starts now!
Course Content
Array Handling
0/1
String Handling
0/1
Wrapper Classes
0/1
Collections in Java
0/1
Packages
0/1
File Handling
0/1
Multithreading
0/1
Java Networking
0/1
Core Java

 

🚀 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!