- Published on
Types of Inheritance in Java & OOP - Single, Multilevel, Hierarchical, Multiple & Hybrid Explained
- ๐ฆ Key Benefits of Inheritance
- 1๏ธโฃ Single Inheritance
- 2๏ธโฃ Multilevel Inheritance
- 3๏ธโฃ Hierarchical Inheritance
- 4๏ธโฃ Multiple Inheritance (via Interfaces in Java)
- 5๏ธโฃ Hybrid Inheritance
- ๐ Comparison Table of Inheritance Types
- ๐ก Real-World Application of Inheritance Types
- โ Interview Questions & Answers
- โ Summary
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 | extends โ extends | 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.