What are the Types of Inheritance in Java? Examples and Tips to Master Inheritance
By Arjun Mathur
Updated on Mar 06, 2025 | 14 min read | 196.8k views
Share:
For working professionals
For fresh graduates
More
By Arjun Mathur
Updated on Mar 06, 2025 | 14 min read | 196.8k views
Share:
Table of Contents
Inheritance in Java lets you build new classes on top of existing ones by reusing and extending fields and methods. For instance, a ‘Car’ class can build upon a ‘Vehicle’ class, carrying over details like speed and color while adding features specific to cars. This approach saves you from writing the same code multiple times.
There are five major types of inheritance in Java, including single-level, multi-level, and hierarchical. Each one caters to unique programming needs, from building basic parent-child links to constructing deeper chains that pass methods through several generations. Understanding the different types of inheritance in Java ensures you can easily handle complex scenarios.
In this blog, you’ll see how each inheritance type works with clear examples. You’ll also discover scenarios where inheritance shines and instances that call for caution.
Inheritance in Java is a principle that allows a new class to borrow attributes and methods from an existing class. It gives you the option to create specialized versions of a broad concept without starting from zero.
Let’s define inheritance in Java with a simple example for better understanding.
Now that you’ve seen how it works, let’s go over a few core terms related to Java inheritance.
Term (Keyword) |
Meaning |
Superclass (Parent Class) | The class whose attributes and methods get inherited. |
Subclass (Child Class) | The class that receives these attributes and methods from the parent class. |
extends | A keyword indicating that one class is inheriting from another. |
super | A keyword used inside a subclass to refer to the parent class, often for calling its constructor or methods. |
Reusability | The advantage of cutting down on repetitive code by using inheritance effectively. |
You can also check out upGrad’s free tutorial on Java: Explore features, applications, types, and many other basic and advanced components.
Inheritance in Java is a key concept that helps classes share fields and methods in a structured way, but different use cases demand different approaches. That’s why there are different types of inheritance in Java to choose from.
Some tasks only need a simple parent-child structure, while others benefit from a chain of classes that build on one another. Selecting the right inheritance type reduces repetitive code and keeps your applications more straightforward to manage.
Here’s a quick snapshot table that explains different types of inheritance in Java at a glance.
Different Types of Inheritance in Java |
Key Characteristics |
Single Inheritance | One parent and one child class. Ideal for simple “is-a” relationships without extra layers. |
Multilevel Inheritance | Inherits through a chain (Grandparent → Parent → Child). Best for step-by-step specialization. |
Hierarchical Inheritance | Multiple child classes share a single parent class. Useful when several classes need common behaviors. |
Multiple Inheritance | Mimicked in Java using interfaces, since a class can implement many interfaces to blend various methods. |
Hybrid Inheritance | Mix of two or more forms (often class-based plus interfaces). Used where no single approach is sufficient. |
Check out upGrad’s free tutorial, The Root Class From Which All Java Classes Got Inherited.
Let’s now take a look at each type in detail and see how it works through examples.
Single inheritance in Java involves a single parent class (also known as a superclass) and a single child class (subclass). By extending one class from another, you reuse methods and fields instead of writing them all over again. This keeps the code cleaner and easier to update.
For instance, if you have a class called Fruit holding general attributes like color and taste, a class called Apple can extend Fruit, automatically receiving these attributes. Changes made in the parent class carry over to the child, minimizing maintenance.
Single inheritance fits situations where you have a direct “is-a” relationship, and no extra layers of specialization are needed.
Single Inheritance in Java with Example
Below is a short code snippet that demonstrates how the Apple class inherits a color field and showColor() method from Fruit, while adding its own method:
class Fruit {
String color = "Red";
void showColor() {
System.out.println("Fruit color: " + color);
}
}
class Apple extends Fruit {
void appleTaste() {
System.out.println("Apple is sweet.");
}
}
public class Main {
public static void main(String[] args) {
Apple myApple = new Apple();
myApple.showColor(); // Inherited method from Fruit
myApple.appleTaste(); // Unique method in Apple
}
}
Output:
Fruit color: Red
Apple is sweet.
Multilevel inheritance in Java places one class beneath another in a straight lineage. A child class inherits from a parent, and then another class can inherit from that child, passing along all previously acquired attributes and methods. This structure is helpful when you need to extend a class step by step because each subclass can build on what was defined before it.
For instance, a Person class might be extended into an Employee class, which then becomes a Manager class. Changes in the topmost parent automatically carry through each level, reducing the amount of code you need to update. It’s especially useful for scenarios where every new class becomes a more specialized version of its parent.
Multilevel Inheritance in Java with Example
Below, a Base class represents a simple Person, which is extended by Employee, and then further extended by Manager. Notice how Manager has access to both Person and Employee features:
class Person {
String name = "Rahul";
void showName() {
System.out.println("Name: " + name);
}
}
class Employee extends Person {
int employeeId = 101;
void showEmployeeId() {
System.out.println("Employee ID: " + employeeId);
}
}
class Manager extends Employee {
String department = "Sales";
void showDepartment() {
System.out.println("Department: " + department);
}
}
public class Main {
public static void main(String[] args) {
Manager mgr = new Manager();
mgr.showName(); // Inherited from Person
mgr.showEmployeeId(); // Inherited from Employee
mgr.showDepartment(); // Unique to Manager
}
}
Output:
Name: Rahul
Employee ID: 101
Department: Sales
Hierarchical inheritance happens when a single parent class has multiple child classes. Each child class inherits shared methods or fields from the parent but keeps its own unique features.
For instance, a generic Vehicle class might hold data like brand and speed, while two subclasses, Car and Bike, each have specialized methods. This setup helps you store common functionality in one place and still allow each child to grow in its own direction. It keeps the code organized, especially if the parent class encapsulates behavior that applies to every child.
Hierarchical Inheritance in Java with Example
Below, the Instrument class serves as a parent, and both Guitar and Piano inherit its showType() method:
class Instrument {
String type = "String Instrument";
void showType() {
System.out.println("Instrument type: " + type);
}
}
class Guitar extends Instrument {
void playGuitar() {
System.out.println("Playing guitar chords.");
}
}
class Piano extends Instrument {
void playPiano() {
System.out.println("Playing piano keys.");
}
}
public class Main {
public static void main(String[] args) {
Guitar gtr = new Guitar();
gtr.showType(); // Inherited from Instrument
gtr.playGuitar(); // Unique to Guitar
Piano pno = new Piano();
pno.showType(); // Inherited from Instrument
pno.playPiano(); // Unique to Piano
}
}
Output:
Instrument type: String Instrument
Playing guitar chords.
Instrument type: String Instrument
Playing piano keys.
Multiple inheritance occurs when one class tries to inherit features from more than one parent class. Java does not allow this directly through classes because it can lead to confusion when two parent classes have methods with the same name. However, you can achieve a similar effect with interfaces.
Each interface can declare methods, and a single class can then implement multiple interfaces. This approach lets you blend functionalities without copying code in multiple places.
Multiple Inheritance in Java with Example
In this example, a Robot class implements two interfaces, Worker and Helper, combining their methods:
interface Worker {
void doWork();
}
interface Helper {
void provideHelp();
}
class Robot implements Worker, Helper {
public void doWork() {
System.out.println("Robot is working.");
}
public void provideHelp() {
System.out.println("Robot is helping.");
}
}
public class Main {
public static void main(String[] args) {
Robot bot = new Robot();
bot.doWork(); // From Worker interface
bot.provideHelp(); // From Helper interface
}
}
Output:
Robot is working.
Robot is helping.
Hybrid inheritance combines more than one type of inheritance in a single design. While Java does not allow multiple class-based inheritance, you can still create hybrid models through interfaces and class hierarchies.
For instance, you can have a parent-child structure using classes, then implement multiple interfaces in one or more subclasses. This gives you the benefits of different inheritance types without the conflicts that come from directly extending multiple parent classes. It’s especially useful in larger programs that need both a clear class chain and modular functionalities from various interfaces.
Hybrid Inheritance in Java with Example
Here, the Base class acts as a parent, two subclasses inherit from it, and one of them implements an extra interface:
class Animal {
void eat() {
System.out.println("Animal eats.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks.");
}
}
interface Pet {
void showAffection();
}
class PetDog extends Dog implements Pet {
public void showAffection() {
System.out.println("Pet dog shows affection.");
}
}
public class Main {
public static void main(String[] args) {
PetDog myDog = new PetDog();
myDog.eat(); // Inherited from Animal
myDog.bark(); // Inherited from Dog
myDog.showAffection(); // From Pet interface
}
}
Output:
Animal eats.
Dog barks.
Pet dog shows affection.
Many Java programs involve classes that share similar data or actions. Without inheritance, you’d end up duplicating these elements in every class, which can lead to confusion and mistakes. Inheritance keeps these shared parts in a single parent class, letting child classes automatically reuse them.
This setup makes it simpler to update or refine common features, since changes in the parent class apply across all children. It also keeps class design more organized, as you can separate general traits from those that are more specialized.
So, what makes inheritance essential in your code? Let’s explore:
Building an inheritance program in Java begins with a parent class with fields and methods you plan to share. Each child class then extends this parent, acquiring those shared elements automatically.
You can override the parent's methods when you want the child class to behave differently. You can also call the parent’s constructor with super() in the child’s constructor, ensuring that any initial setup in the parent is carried out correctly.
Here’s a simple step-by-step approach:
Below is a short example that puts these steps into practice.
There’s a parent class that sets a name and provides a greet method. The child class extends it, calls the parent’s constructor with super, overrides the greet method, and adds its own method.
class Person {
String name;
Person(String name) {
this.name = name;
}
void greet() {
System.out.println("Hello, my name is " + name);
}
}
class Teacher extends Person {
String subject;
Teacher(String name, String subject) {
super(name); // Calls Person's constructor
this.subject = subject;
}
@Override
void greet() {
System.out.println("Hello, I am " + name + ", and I teach " + subject);
}
void teach() {
System.out.println("Teaching now!");
}
}
public class Main {
public static void main(String[] args) {
Teacher t = new Teacher("Priya", "Mathematics");
t.greet(); // Overridden method
t.teach(); // Unique method in Teacher
}
}
Output:
Hello, I am Priya, and I teach Mathematics
Teaching now!
Did you know that around 30.3% of professional developers worldwide rely on Java for their day-to-day programming? It’s the 7th most popular programming language in the world. One reason behind Java’s continued appeal is how its inheritance features keep code organized and adaptable.
Here are some key advantages that come from using different types of inheritance in Java:
Also Read: Polymorphism In OOPS: What is Polymorphism [Detailed Explanation]
upGrad’s Exclusive Software and Tech Webinar for you –
SAAS Business – What is So Different?
Although inheritance reduces code repetition, it can also introduce hidden complexity if not planned carefully. A deeply nested chain of classes might confuse anyone new to your codebase. Modifying a single parent class might also have unintended effects on multiple child classes at once.
Below are some drawbacks to watch out for when using inheritance in Java:
Now that you’ve seen where different types of inheritance in Java can go wrong, it’s time to focus on ways to avoid those pitfalls. Proper planning and a clear understanding of inheritance can keep your code efficient, maintainable, and free from surprises.
Below are a few suggestions that go beyond the basics:
You can also explore upGrad’s free tutorial on Final Class in Java. Understand the methods and advantages, and learn how to create final classes with ease.
Learning inheritance and the types of inheritance in Java open doors to advanced development skills and career opportunities. upGrad offers comprehensive learning programs to ensure you gain theoretical knowledge and hands-on experience.
If you’re ready to dive deeper into Java programming, here are some curated programs offered by upGrad:
Don’t let the complexities of inheritance hold you back. With upGrad’s expert guidance, you can confidently figure out your interests and the right path. Book your career counseling session today and find the perfect course for your goals!
Related Blogs:
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
Reference Link:
https://www.statista.com/statistics/793628/worldwide-developer-survey-most-used-languages/
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources