Inheritance in Java is one of the most important Object-Oriented Programming (OOP) concepts. It allows developers to reuse existing code, create clear class hierarchies, and build scalable applications without rewriting logic again and again.
If you want to understand Java deeply—especially for interviews, backend development, or Spring Boot—inheritance is non-negotiable.
This guide explains inheritance in Java from basics to advanced, covering definitions, types, rules, examples, and real-world usage in a clear and beginner-friendly way.
What Is Inheritance in Java?
Inheritance is a mechanism in Java where one class acquires the properties and methods of another class.
- The class that provides data and methods is called the Superclass (Parent)
- The class that inherits them is called the Subclass (Child)
In Java, inheritance is implemented using the extends keyword.
Inheritance helps in:
- Code reusability
- Faster development
- Better readability
- Logical class hierarchy
Basic Syntax of Inheritance in Java
class Parent {
void show() {
System.out.println("This is parent class");
}
}
class Child extends Parent {
void display() {
System.out.println("This is child class");
}
}
Here, the Child class automatically gets access to the show() method of Parent.
Why Inheritance Is Important in Java
Inheritance is not just a theoretical concept—it is heavily used in real-world Java applications.
Key benefits:
- Avoids duplicate code
- Supports extensibility
- Improves maintainability
- Enables polymorphism
- Forms the foundation of frameworks like Spring
Most Java APIs and frameworks rely on inheritance internally.
Types of Inheritance in Java
Java supports five conceptual types of inheritance, but not all are allowed directly using classes.
Let’s understand each one clearly.
1. Single Inheritance
Definition:
When one class inherits another single class.
Example:
class Bird {
void fly() {
System.out.println("I can fly");
}
}
class Parrot extends Bird {
void color() {
System.out.println("I am green");
}
}
✔ Supported in Java
✔ Simple and most commonly used
2. Multilevel Inheritance
Definition:
When a class is derived from another derived class.
Example:
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Bird extends Animal {
void fly() {
System.out.println("Flying");
}
}
class Parrot extends Bird {
void speak() {
System.out.println("Speaking");
}
}
✔ Supported in Java
✔ Used when behavior builds layer by layer
3. Hierarchical Inheritance
Definition:
When multiple subclasses inherit the same superclass.
Example:
class Vehicle {
void run() {
System.out.println("Vehicle running");
}
}
class Car extends Vehicle {}
class Bike extends Vehicle {}
✔ Supported in Java
✔ Very common in real-world systems
4. Multiple Inheritance (Classes)
Definition:
When one class inherits from more than one class.
Not supported in Java using classes
Reason:
It causes ambiguity (Diamond Problem).
Java intentionally disallows this to keep the language safe and predictable.
5. Hybrid Inheritance
Definition:
Combination of multiple and hierarchical inheritance.
Not supported directly using classes
✔ Possible using interfaces
Java allows hybrid inheritance only through interfaces, not classes.
Multiple Inheritance Using Interfaces (Java Special Case)
Java solves multiple inheritance issues using interfaces.
interface LandVehicle {
void drive();
}
interface WaterVehicle {
void sail();
}
class AmphibiousVehicle implements LandVehicle, WaterVehicle {
public void drive() {}
public void sail() {}
}
✔ Safe
✔ No ambiguity
✔ Widely used in enterprise applications
Important Rules of Inheritance in Java
- Java supports single inheritance with classes
- Multiple inheritance is allowed only via interfaces
- Private members are not inherited
- Constructors are not inherited
superkeyword is used to access parent members- Method overriding is allowed
- Fields are hidden, methods are overridden
Inheritance vs Composition (Quick Insight)
While inheritance promotes reuse, composition is preferred in many modern designs.
Use inheritance when:
- There is a strong IS-A relationship
Use composition when:
- Flexibility is required
- Behavior may change
Good Java developers know when not to use inheritance.
Real-World Use of Inheritance in Java
Inheritance is used extensively in:
- Spring Boot controllers and services
- Exception handling (
RuntimeException) - Collection framework (
ArrayList,List) - Java I/O classes
- REST APIs











