Types of Inheritance in Java: Examples and Tips
By Arjun Mathur
Updated on Dec 05, 2025 | 14 min read | 199.15K+ views
Share:
Working professionals
Fresh graduates
More
By Arjun Mathur
Updated on Dec 05, 2025 | 14 min read | 199.15K+ views
Share:
Table of Contents
Did You Know? Minecraft Java Edition is the most iconic Java-powered game, written entirely in Java and has sold over 200 million copies.
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.
Explore data science online courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
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, in hybrid inheritance in Java, 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.
Upgrade your tech skills for tomorrow's challenges! Explore our free IT & Technology course and stay ahead in the digital era.
Begin Your Free Course!
Master Java from Scratch – Free 10-Hour Course!
Start your programming journey with this beginner-friendly Java Full Course for 2025 — perfect for students and aspiring developers.
📺 Watch Now: Java Full Course for Beginners 2025 | Java Tutorial for Beginners | 10 Hours Free Java Full Course
Software Development Courses to upskill
Explore Software Development Courses for Career Progression
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!
Discover the Origins of Java Programming!
Start your coding journey with History & Introduction to Java — perfect for beginners eager to understand what makes Java so powerful and popular.
📺 Watch Now: History of Java | Introduction to Java | What is Java? | Java Tutorial for Beginners Part 1
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?
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
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/
Hybrid inheritance is not directly supported by Java classes but represents a common design pattern combining two or more types of inheritance in Java, typically single and hierarchical, often using interfaces. It allows for complex, specialized class structures, managing the challenge of the rigid single-parent restriction. Understanding hybrid inheritance in Java is key to mastering advanced OOP architecture and utilizing the full power of all available type of inheritance in Java constructs.
Multilevel inheritance in Java creates a linear chain, where a class (Child) inherits from another class (Parent), which itself inherits from a third (Grandparent). This specific type of inheritance in Java is crucial for establishing clear step-by-step specialization, allowing the Child to access methods and properties from the entire ancestry. It is a fundamental type of inheritance in Java that enables deep code reusability and structured development.
Java avoids implementing multiple inheritance for classes to prevent the "diamond problem," a situation where a child class inherits the same method from two different parents, causing ambiguity. This design choice ensures reliability and avoids complex conflicts in the class structure. However, Java enables the functionality of multiple inheritance indirectly through the use of interfaces, allowing a class to inherit diverse behaviors and maintain clarity in its types of inheritance in Java.
A constructor is a specialized method used solely for initializing a new object upon its creation. It must share the exact name of the class and has no explicit return type. Constructors are automatically called by the Java Virtual Machine (JVM) when the new keyword is utilized. For classes involved in inheritance, constructors play a vital role in setting up the initial state of both the child and parent components, regardless of the specific type of inheritance in Java being used.
No, constructors are not inherited in Java. However, a child class is required to call its parent's constructor to properly initialize the inherited instance members. This call is made using the super() keyword, which must be the first statement in the child constructor. This mechanism ensures that the parent class is correctly set up before the child class initializes its own specific components in any multilevel inheritance in Java structure.
Overriding is an Object-Oriented Programming (OOP) feature where a child class provides its own specific implementation for a method that is already defined in its parent class. The method signature must remain identical, but the implementation logic differs. This is key to achieving runtime polymorphism and is utilized across different types of inheritance in Java to allow specialized class behavior while maintaining a consistent public interface.
Both are foundational types of inheritance in Java. Single inheritance involves one parent extending to one child class, establishing a direct one-to-one relationship. In contrast, hierarchical inheritance involves one single parent class having multiple independent child classes. This structural difference is vital: single is for linear extension, while hierarchical is for sharing common behavior across many distinct sub-classes in the overall types of inheritance in Java model.
The final keyword is a non-access modifier used to impose restrictions on a variable, method, or class. A final variable's value cannot change; a final method cannot be overridden in any child class; and a final class cannot be extended, effectively stopping the possibility of further inheritance. This is essential for achieving security and immutability in Java development, regardless of the complexity of multilevel inheritance in Java.
Yes, a final method can be inherited by a child class. Inheritance means the child class gains access to the method's functionality and can call it. However, the crucial restriction is that the child class cannot override the method to change its behavior. This feature allows for the reuse of crucial, unchangeable logic within various types of inheritance in Java while guaranteeing its consistency across the class hierarchy.
Abstraction is a core OOP concept that focuses on showing essential information to the user while hiding complex, internal implementation details. In Java, this is primarily achieved using abstract classes and interfaces. Abstraction is a critical complement to the types of inheritance in Java, enabling developers to enforce specific functional contracts and create robust, easily maintainable codebases by separating the 'what' (interface) from the 'how' (implementation details).
Yes, if you do not explicitly include a constructor in a child class, the Java compiler automatically inserts a call to the parent class's parameterless constructor, super(), as the first statement. If the parent class only has parameterized constructors, you must manually call the appropriate super() version. This default mechanism ensures proper initialization of the parent's components within all types of inheritance in Java.
The Object class is the root of the Java class hierarchy. Every class in Java, whether explicitly declared or not, is directly or indirectly a subclass of Object. This means every class automatically inherits fundamental methods like equals(), hashCode(), and toString(). This common ancestor ensures a standard base level of functionality for all objects within the robust Java system, regardless of their specific type of inheritance in Java.
Interfaces are primarily used to achieve polymorphism and to allow multiple inheritance of behavior in Java, as classes only support single class inheritance. An interface defines a contract, a set of methods a class must implement, without providing implementation details. This mechanism is crucial for designing loose coupling between components and is a key part of creating hybrid inheritance in Java structures.
No, a private method cannot be overridden. Private methods are only accessible within their own class, meaning they are not considered part of the class's public interface or inherited by subclasses. If a child class defines a method with the same signature as a private parent method, it is treated as a completely new method in the child class, not an override.
If an interface method is implemented in a concrete class without explicitly using the public keyword, the Java compiler will generate an error. All methods inherited from an interface are implicitly public and abstract. Therefore, the implementing class must ensure the overridden method is declared as public, adhering to the rules of method overriding across different types of inheritance in Java for proper access control.
The instanceof operator is used to check if an object is an instance of a particular class or implements a specific interface. This operator is invaluable in inheritance hierarchies, particularly when dealing with polymorphism, as it allows developers to safely determine an object’s actual type at runtime before casting. This ensures reliable code execution in complex scenarios involving multilevel inheritance in Java.
Inheritance is the formal mechanism Java uses to express the "is-a" relationship, the foundational principle of object-oriented modeling. For instance, if a Car class extends a Vehicle class, we say "A Car is a Vehicle." This relationship is vital for ensuring that a subclass is functionally compatible with its superclass, forming the logical basis for all types of inheritance in Java and achieving robust polymorphism.
An abstract method is a declaration without a body. If a subclass inherits an abstract method and is the first concrete class (i.e., not declared abstract itself), it must provide an implementation for that method. Failure to do so would leave the method body undefined, preventing the class from being instantiated. This rule enforces the contracts defined in abstract classes across all type of inheritance in Java hierarchies.
The protected access modifier allows fields or methods to be accessible within their own package and by all subclasses, even those in different packages. This is a crucial feature in inheritance, as it provides a middle ground: better encapsulation than public while still allowing subclasses (like those in multilevel inheritance in Java) to access and utilize members necessary for their specific functionality.
You should prefer composition ("has-a" relationship) over inheritance ("is-a" relationship) when a subclass doesn't truly represent a specialized form of the superclass. Composition involves including an object of another class as a member, offering greater flexibility by allowing behavior to be changed dynamically at runtime, whereas the behavior granted by types of inheritance in Java is static and fixed at compile time.
57 articles published
Arjun Mathur is Program Marketing Manager for the Software Development, bringing over 5+ years of experience in ed‑tech and growth marketing. A B.Tech in Computer Engineering from IIT Delhi, he specia...
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