🚀 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:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
⚠️ Note: Java does not support multiple inheritance with classes (to avoid ambiguity) but it supports it through interfaces.
1️⃣ Single Inheritance
-
One class inherits from another.
-
Example: A
Dogclass inherits fromAnimal.
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:
CatandDogboth inherit fromAnimal.
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!