Logo
Published on

Types of Inheritance in Java & OOP - Single, Multilevel, Hierarchical, Multiple & Hybrid Explained

Inheritance is an OOP concept where one class (subclass) can inherit fields and methods from another class (superclass) — enabling code reuse, polymorphism, and modular design.

Java supports different types of inheritance, either directly or through interfaces. Understanding these types is essential for designing flexible and scalable applications.

📦 Key Benefits of Inheritance

  • ✅ Promotes code reuse
  • ✅ Enables hierarchical classification
  • ✅ Supports polymorphism
  • ✅ Encourages DRY (Don't Repeat Yourself) principles
  • ✅ Makes code scalable and extensible

1️⃣ Single Inheritance

🧠 Definition:

A subclass inherits from one superclass.

🔗 Syntax Example:

class Animal {
    void eat() { System.out.println("Eating..."); }
}

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

✅ Key Points:

  • Simplest form of inheritance
  • Promotes direct reuse of behavior
  • Java supports this directly

🌍 Analogy:

Bike inherits from Vehicle — It's a single path of inheritance.

2️⃣ Multilevel Inheritance

🧠 Definition:

A class is derived from a derived class (a chain of inheritance).

🔗 Syntax Example:

class Animal {
    void eat() { System.out.println("Eating"); }
}

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

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

✅ Key Points:

  • Involves a hierarchy of inheritance
  • Each level builds upon the previous one
  • Java supports it directly

🌍 Analogy:

Child → Parent → Grandparent

3️⃣ Hierarchical Inheritance

🧠 Definition:

Multiple subclasses inherit from a single superclass.

🔗 Syntax Example:

class Animal {
    void eat() { System.out.println("Eating"); }
}

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

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

✅ Key Points:

  • Common behavior in superclass
  • Individual behavior in each subclass
  • Useful for shared features across entities

🌍 Analogy:

Vehicle → Car, Truck, Bike

4️⃣ Multiple Inheritance (via Interfaces in Java)

🧠 Definition:

A class inherits features from multiple sources. Java doesn't support it with classes but achieves it using interfaces.

🔗 Syntax Example:

interface Printable {
    void print();
}

interface Showable {
    void show();
}

class Document implements Printable, Showable {
    public void print() { System.out.println("Printing..."); }
    public void show() { System.out.println("Showing..."); }
}

✅ Key Points:

  • Java avoids multiple class inheritance to prevent diamond problem
  • Interfaces allow multiple behavior contracts
  • Promotes flexibility and abstraction

🌍 Analogy:

Smartphone implements Camera + MusicPlayer

5️⃣ Hybrid Inheritance

🧠 Definition:

A combination of two or more types of inheritance (e.g., single + multiple, multilevel + hierarchical).

🔗 Syntax Example (Using Interfaces):

interface A {
    void methodA();
}

interface B {
    void methodB();
}

class Parent {
    void methodParent() {
        System.out.println("Parent Method");
    }
}

class Child extends Parent implements A, B {
    public void methodA() { System.out.println("Method A"); }
    public void methodB() { System.out.println("Method B"); }
}

✅ Key Points:

  • Not directly supported with classes in Java
  • Achievable through interfaces
  • Common in real-world applications and frameworks

🌍 Analogy:

Electric Car → Inherits from Car (class) + Implements Chargeable, Drivable (interfaces)

🔁 Comparison Table of Inheritance Types

Type Supported in Java? Example Keyword(s) Description
Single ✅ Yes extends One class inherits another
Multilevel ✅ Yes extendsextends Chain of inheritance
Hierarchical ✅ Yes Multiple extends from one class One superclass, many subclasses
Multiple ⚠️ Yes (via interfaces) implements One class implements multiple interfaces
Hybrid ✅ With interfaces Mix of extends + implements Combination of any above types

💡 Real-World Application of Inheritance Types

Scenario Inheritance Type
Animal classification Hierarchical
E-commerce User → Admin → SuperAdmin Multilevel
PaymentGateway implements PayPal + Stripe Multiple via Interfaces
Vehicle extends Transport + implements Drivable Hybrid

❓ Interview Questions & Answers

Q1. Why doesn't Java support multiple inheritance with classes? A: To avoid ambiguity (Diamond Problem). Interfaces solve this cleanly.

Q2. How does Java achieve multiple inheritance? A: Using interfaces with the implements keyword.

Q3. What's the difference between hierarchical and multilevel inheritance? A: Hierarchical has one parent with multiple children; Multilevel is a chain of inheritance.

Q4. What is the diamond problem? A: Ambiguity caused when a class inherits the same method from multiple classes. Java avoids this by disallowing multiple class inheritance.

Q5. When would you use hybrid inheritance? A: In complex systems requiring both shared class behavior and multiple interfaces.

✅ Summary

  • Inheritance is a core concept in OOP for code reuse, hierarchy, and polymorphism.
  • Java supports Single, Multilevel, Hierarchical, and Hybrid inheritance.
  • Multiple inheritance is supported through interfaces, not classes.
  • A strong grasp of inheritance types is critical for software design and interviews.