๐ Introduction
In the previous article of this inheritance series, we learned the basic concept of inheritance in Javaโhow a class can inherit properties and behaviors from another class using the extends keyword.
Inheritance allows programmers to:
- Reuse existing code
- Reduce redundancy
- Build structured applications
- Represent real-world relationships in programs
However, inheritance relationships between classes can be structured in different ways. Depending on how classes are connected in a hierarchy, inheritance can take several forms.
These forms are called Types of Inheritance.
Understanding these types is very important because they help developers:
- Design better class hierarchies
- Avoid common design problems
- Understand why Java supports some inheritance patterns but restricts others
In this article, we will cover:
- All types of inheritance
- Java-supported inheritance structures
- Examples with Java code
- Important rules and limitations
- Common beginner mistakes
By the end of this article, you will have a clear understanding of how inheritance structures work in Java.
๐ง What Are Types of Inheritance?
Types of inheritance refer to the different ways classes can be related using inheritance.
In object-oriented programming, inheritance structures create hierarchies of classes.
There are five commonly discussed types of inheritance:
1๏ธโฃ Single Inheritance
2๏ธโฃ Multilevel Inheritance
3๏ธโฃ Hierarchical Inheritance
4๏ธโฃ Multiple Inheritance
5๏ธโฃ Hybrid Inheritance
However, Java supports only three of them directly with classes.
Supported in Java:
โ Single Inheritance
โ Multilevel Inheritance
โ Hierarchical Inheritance
Not supported with classes:
โ Multiple Inheritance
โ Hybrid Inheritance
Let us understand each type step by step.
๐ฑ Single Inheritance
๐งพ Definition
Single inheritance occurs when one child class inherits from exactly one parent class.
Structure:
Parent Class
โ
Child Class
This is the simplest form of inheritance.
๐ป Example of Single Inheritance
class Animal {
void eat() {
System.out.println("Animals eat food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
๐ Code Explanation
First class:
class Animal
This is the parent class.
Method inside parent class:
void eat()
This method represents a behavior common to animals.
Child class declaration:
class Dog extends Animal
The Dog class inherits from Animal.
Because of inheritance, Dog automatically gets access to the eat() method.
Dog-specific method:
void bark()
This method belongs only to the Dog class.
Object creation:
Dog d = new Dog();
This creates a Dog object.
Method calls:
d.eat();
d.bark();
The Dog object can call:
- Parent method โ
eat() - Child method โ
bark()
๐งพ Output
Animals eat food
Dog barks
๐ฟ Multilevel Inheritance
๐งพ Definition
Multilevel inheritance occurs when a class inherits from another class, which itself inherits from another class.
Structure:
Grandparent
โ
Parent
โ
Child
๐ป Example of Multilevel Inheritance
class Animal {
void eat() {
System.out.println("Animals eat food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps");
}
}
public class Main {
public static void main(String[] args) {
Puppy p = new Puppy();
p.eat();
p.bark();
p.weep();
}
}
๐ Code Explanation
Hierarchy:
Animal โ Dog โ Puppy
Explanation:
Doginherits fromAnimalPuppyinherits fromDog
Therefore the Puppy class gets access to:
โ eat() from Animal
โ bark() from Dog
โ weep() from Puppy
๐งพ Output
Animals eat food
Dog barks
Puppy weeps
โ ๏ธ Common Beginner Confusion
Many students think:
Puppy inherits only Dog methods.
This is incorrect.
Puppy inherits both Dog and Animal methods.
๐ณ Hierarchical Inheritance
๐งพ Definition
Hierarchical inheritance occurs when multiple child classes inherit from the same parent class.
Structure:
Animal
/ \
Dog Cat
๐ป Example of Hierarchical Inheritance
class Animal {
void eat() {
System.out.println("Animals eat food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
d.eat();
d.bark();
c.eat();
c.meow();
}
}
๐ Explanation
Here:
- Dog inherits from Animal
- Cat also inherits from Animal
Both classes share the eat() method from the parent class.
๐งพ Output
Animals eat food
Dog barks
Animals eat food
Cat meows
๐ซ Multiple Inheritance (Not Supported in Java Classes)
๐งพ Definition
Multiple inheritance occurs when a child class inherits from more than one parent class.
Structure:
Parent1 Parent2
\ /
Child
Example (not allowed in Java):
class A {
}class B {
}class C extends A, B {
}
This will cause a compile-time error.
๐ง Why Java Does Not Support Multiple Inheritance
The main reason is the Diamond Problem.
Example structure:
A
/ \
B C
\ /
D
If both B and C override a method from A, then class D cannot determine:
Which method should it inherit?
This ambiguity is called the Diamond Problem.
To avoid this issue, Java does not allow multiple inheritance with classes.
However, Java supports multiple inheritance through interfaces, which we will discuss later in the series.
๐ท Hybrid Inheritance
Hybrid inheritance is a combination of two or more inheritance types.
Example:
Single + Hierarchical
Java does not support hybrid inheritance with classes, but it can be implemented using interfaces.
โ ๏ธ Common Mistakes Students Make
โ Mistake 1: Forgetting the extends keyword
Without extends, inheritance does not occur.
โ Mistake 2: Thinking parent classes can access child methods
Parent classes cannot access child class members.
Example:
Animal cannot call bark()
โ Mistake 3: Confusing multilevel inheritance with multiple inheritance
Multilevel:
A โ B โ C
Multiple:
A
\
C
/
B
โ Mistake 4: Accessing private variables from child class
Private members are not accessible directly in child classes.
๐ง Quick Summary Table
| Type | Supported in Java Classes |
|---|---|
| Single | โ Yes |
| Multilevel | โ Yes |
| Hierarchical | โ Yes |
| Multiple | โ No |
| Hybrid | โ No |
๐ Key Points to Remember
โ Inheritance helps create class hierarchies
โ Java supports three types directly
โ Multiple inheritance causes ambiguity
โ Interfaces allow multiple inheritance in Java
โ Understanding inheritance structures helps design better programs
๐ Conclusion
Inheritance is a powerful concept in Java that allows developers to build programs using hierarchical relationships between classes. It enables code reuse, better organization, and improved maintainability.
In this article, we explored the different types of inheritance, including:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
We also understood why Java does not support multiple inheritance with classes and how this restriction prevents ambiguity in programs.
Understanding these inheritance types is essential for designing clean and efficient object-oriented systems.
In the next article of this inheritance series, we will explore:
๐ Method Overriding in Java โ How a child class modifies the behavior of a parent class method.
This concept is extremely important because it introduces runtime polymorphism, which is one of the most powerful features of Java.
Mastering it will take your understanding of object-oriented programming to the next level.