What is Inheritance?
Inheritance is a mechanism in Java where one class acquires the properties and behaviors of another class.
- The class whose properties are inherited is called the Parent / Super class
- The class that inherits the properties is called the Child / Sub class
Inheritance represents an IS-A relationship.
Why Use Inheritance?
Inheritance is used to:
- Reuse existing code
- Avoid code duplication
- Improve maintainability
- Create a parent-child relationship between classes
Syntax of Inheritance
class Parent {
// variables and methods
}
class Child extends Parent {
// child class code
}
The keyword extends is used to achieve inheritance in Java.
Types of Inheritance in Java
Java supports five types of inheritance conceptually, but only three are supported using classes.
1. Single Inheritance
What is Single Inheritance?
A single child class inherits from one parent class.
Structure:
Parent → Child
Example:
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
Here, Dog inherits the eat() method from Animal.
2. Multilevel Inheritance
What is Multilevel Inheritance?
A class inherits from another class, which itself inherits from another class.
Structure:
Grandparent → Parent → Child
Example:
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps");
}
}
Here, Puppy can access methods from both Dog and Animal.
3. Hierarchical Inheritance
What is Hierarchical Inheritance?
Multiple child classes inherit from one parent class.
Structure:
Parent
/ \
Child1 Child2
Example:
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
}
}
Here, both Dog and Cat inherit from Animal.
4. Multiple Inheritance (Not Supported with Classes)
What is Multiple Inheritance?
A single class inherits from more than one parent class.
Example (Not Allowed):
class A { }
class B { }
class C extends A, B { } // ❌ Invalid
Java does not support multiple inheritance using classes to avoid ambiguity (Diamond Problem).
5. Hybrid Inheritance (Not Supported with Classes)
Hybrid inheritance is a combination of multiple inheritance types.
Java does not support hybrid inheritance using classes, but it can be achieved using interfaces.
Summary of Inheritance Support in Java
| Type | Supported Using Classes |
| ------------ | ----------------------- |
| Single | Yes |
| Multilevel | Yes |
| Hierarchical | Yes |
| Multiple | No |
| Hybrid | No |
Important Points to Remember
- Inheritance uses the
extendskeyword - Java supports only single inheritance with classes
- Parent class members are accessible in the child class
- Constructors are not inherited
- Inheritance promotes code reusability
When to Use Inheritance?
Use inheritance when:
- There is a clear IS-A relationship
- Child class needs parent class behavior
- Code reuse is required
Key Points Summary
- Inheritance allows one class to reuse another class’s code
- It creates a parent-child relationship
- Java supports single, multilevel, and hierarchical inheritance
- Multiple inheritance is not supported using classes
Top comments (0)