Java Inheritance Types – Complete Guide with Examples
Learn all types of inheritance in Java, including single, multilevel, hierarchical, and hybrid inheritance, with detailed examples and best practices in object-oriented programming.
Inheritance Types in Java – Complete Detailed Tutorial
Inheritance is a key feature of Object-Oriented Programming (OOP).
It allows a class (child/subclass) to inherit fields and methods from another class (parent/superclass).
This promotes code reuse and modularity.
1. Advantages of Inheritance
- Code Reusability: Use existing methods and fields in new classes
- Method Overriding: Customize inherited methods
- Extensibility: Easy to extend programs
- Data Hiding: Parent class can restrict access using access modifiers
2. Syntax of Inheritance
extendskeyword is used to inherit a class- Java supports single, multilevel, hierarchical inheritance
- Multiple inheritance with classes is not supported (avoids ambiguity)
- Interfaces can provide multiple inheritance
3. Types of Inheritance in Java
3.1 Single Inheritance
- A child class inherits from a single parent class
Example – Single Inheritance:
Output:
3.2 Multilevel Inheritance
- A class inherits from a child class, forming a chain
Example – Multilevel Inheritance:
Output:
3.3 Hierarchical Inheritance
- Multiple child classes inherit from a single parent class
Example – Hierarchical Inheritance:
Output:
3.4 Multiple Inheritance (Through Interfaces)
- Java does not support multiple inheritance with classes to avoid ambiguity
- Interfaces allow multiple inheritance
Example – Multiple Inheritance via Interface:
Output:
3.5 Hybrid Inheritance (Combination)
- Combination of multiple inheritance types
- Achieved using interfaces to avoid class-based multiple inheritance
Example:
Output:
4. Summary of Inheritance Types
| TypeDescription | |
| Single | One child inherits from one parent |
| Multilevel | Chain of inheritance (grandparent → parent → child) |
| Hierarchical | Multiple children inherit from one parent |
| Multiple (via Interface) | One class implements multiple interfaces |
| Hybrid (Combination) | Combination of above types using classes and interfaces |
Key Points:
- Promotes code reuse and modularity
- Avoids redundancy
- Method overriding works with inheritance