If you are learning Java OOP concepts, one of the most important topics is inheritance. Many students are asked to write a program in Java to demonstrate single multilevel and hierarchical inheritance in exams and assignments.
But the real challenge is understanding how these inheritance types actually work in code. In this guide, you will learn all three types with simple examples, clean code, and clear explanations.
This article is beginner-friendly and helps you understand how classes reuse properties and methods using inheritance.
Concept Explanation
Inheritance in Java allows one class to acquire properties and methods of another class. It helps in code reusability and improves structure.
There are three main types covered in this program:
- Single Inheritance: One child class inherits from one parent class.
- Multilevel Inheritance: A class inherits from another child class.
- Hierarchical Inheritance: Multiple classes inherit from a single parent class.
Understanding these types is essential when you write a program in Java to demonstrate single multilevel and hierarchical inheritance.
Program Code
1. Single Inheritance Example
class Animal {
String name = "Dog";
}
class Dog extends Animal {
String breed = "German Shepherd";
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
System.out.println(d.name);
System.out.println(d.breed);
}
}
2. Multilevel Inheritance Example
class Animal {
String name = "Dog";
}
class Dog extends Animal {
String type = "Mammal";
}
class GermanShepherd extends Dog {
String breed = "German Shepherd";
}
public class Main {
public static void main(String[] args) {
GermanShepherd g = new GermanShepherd();
System.out.println(g.name);
System.out.println(g.type);
System.out.println(g.breed);
}
}
3. Hierarchical Inheritance Example
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String sound = "Bark";
}
class Cat extends Animal {
String sound = "Meow";
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
System.out.println(d.name + " makes " + d.sound);
Cat c = new Cat();
System.out.println(c.name + " makes " + c.sound);
}
}
Sample Output
Dog German Shepherd Dog Mammal German Shepherd Animal makes Bark Animal makes Meow
Step-by-Step Explanation
- In single inheritance, Dog inherits properties from Animal.
- In multilevel inheritance, GermanShepherd inherits from Dog, which already inherits from Animal.
- In hierarchical inheritance, both Dog and Cat inherit from the same Animal class.
- This shows how one parent class can be reused in different ways.
So when you write a program in Java to demonstrate single multilevel and hierarchical inheritance, you are basically showing how classes are connected.
Time Complexity
The time complexity of these programs is O(1) because there are no loops or heavy computations. The operations are simple object creation and printing values.
Real-World Applications
- Banking systems (Account → Savings, Current)
- Vehicle systems (Vehicle → Car, Bike)
- Employee management systems
- Game character design (Base character → different roles)
Common Mistakes
- Confusing multilevel with hierarchical inheritance
- Forgetting to use
extendskeyword - Creating unnecessary duplicate variables
- Not understanding parent-child relationship
Best Practices
- Keep class hierarchy simple and meaningful
- Avoid deep inheritance unless necessary
- Use proper naming for classes
- Write reusable code
Frequently Asked Questions
1. What is inheritance in Java?
Inheritance allows one class to use properties and methods of another class.
2. What is single inheritance?
It is when one class inherits from one parent class.
3. What is multilevel inheritance?
It is when a class inherits from another child class.
4. What is hierarchical inheritance?
Multiple classes inherit from one parent class.
5. Why is inheritance important?
It reduces code duplication and improves program structure.
🔗 Related Articles
- Java Method Overriding Explained
- Multiple Inheritance Using Interfaces
- Abstract Class Example in Java
- Final Class in Java Explained
Conclusion
Now you clearly understand how to write a program in Java to demonstrate single multilevel and hierarchical inheritance. These are fundamental concepts in Java that every beginner must master.
Practice these examples and try modifying them to build your own class structures. Once you understand inheritance, you will find object-oriented programming much easier and more powerful.

Comments
Post a Comment