Inheritance in Java

Last Updated : 2 Jan 2026

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).

The idea behind inheritance in Java is that we can create new classes that are built upon existing classes. When we inherit methods from an existing class, we can reuse methods and fields of the parent class. However, we can add new methods and fields in your current class also.

What is Inheritance?

Inheritance in Java enables a class to inherit properties and actions from another class, called a superclass or parent class. A class derived from a superclass is called a subclass or child group. Through inheritance, a subclass can access members of its superclass (fields and methods), enforce reuse rules, and encourage hierarchy.

Inheritance represents the IS-A relationship which is also known as a parent-child relationship.

Why Use Inheritance in Java?

The inheritance is used for the following important reasons:

  • Method Overriding: Inheritance allows a subclass to provide a specific implementation of a method already defined in its superclass. This is also known as runtime polymorphism.
  • Code Reusability: Inheritance allows developers to reuse the existing code from a parent class which reduces the redundancy and makes the code easier to maintain.

Terms used in Inheritance

Here are the important terms (terminologies) that are used in inheritance:

  • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
  • Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
  • Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.

The extends Keyword

The extends keyword is used to create a new class (subclass) that derives from an existing class (superclass). It allows the subclass to inherit fields and methods from the superclass.

Syntax

It has the following syntax:

Java Inheritance Example

Inheritance in Java

As displayed in the above figure, Programmer is the subclass and Employee is the superclass. The relationship between the two classes is Programmer IS-A Employee. It means that Programmer is a type of Employee.

This example demonstrate the single inheritance in Java, where the Programmer class inherits the salary variable from the Employee class and also defines its own bonus.

Example

Compile and Run

Output:

 Programmer salary is:40000.0
 Bonus of programmer is:10000

In the above example, Programmer object can access the field of own class as well as of Employee class i.e. code reusability.

Types of Inheritance

There are several types of inheritance supported in Java:

  1. Single Inheritance:
    Single inheritance allows a subclass inherits from one superclass only.
  2. Multilevel Inheritance:
    Multilevel inheritance allows a class is derived from a subclass, forming a chain of inheritance.
  3. Hierarchical Inheritance:
    Hierarchical inheritance allows multiple subclasses inherit from the same superclass.
  4. Hybrid Inheritance (Through Interfaces):
    Hybrid inheritance combines two or more types of inheritance using interfaces to avoid multiple inheritance issues.

In Java, multiple inheritance and hybrid inheritance are supported only through interfaces, because Java does not allow multiple inheritance with classes directly. We will learn more about interfaces later.

Single Inheritance

When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.

Inheritance in Java

Let us take an example to demonstrate the single inheritance in Java.

Example

Compile and Run

Output:

barking...
eating...

Multilevel Inheritance

When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.

Inheritance in Java
 

Here, we are going to take an example to demonstrate the working of multilevel inheritance in Java.

Example

Compile and Run

Output:

weeping...
barking...
eating...

Hierarchical Inheritance

When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.

Inheritance in Java
 

Here, we are going to take an example to demonstrate the working of hierarchical inheritance in Java.

Example

Compile and Run

Output:

meowing...
eating...

Multiple Inheritance

Multiple Inheritance A class's capacity to inherit traits from several classes is referred to as multiple inheritances. This notion may be quite helpful when a class needs features from many sources.

Inheritance in Java

However, Multiple inheritances can result in issues like the diamond problem, which occurs when two superclasses share the same method or field and causes conflicts. Java uses interfaces to implement multiple inheritances in order to prevent these conflicts.

Here, we are going to take an example to demonstrate the working of multiple inheritance in Java.

Example

Compile and Run

Output:

Warrior attacks with a sword.
Warrior uses a sword.
Mage attacks with a wand.
Mage uses a wand.

Hybrid Inheritance in Java

The hybrid inheritance is the composition of two or more types of inheritance. The main purpose of using hybrid inheritance is to modularize the code into well-defined classes. It also provides the code reusability.

Inheritance in Java

The hybrid inheritance can be achieved by using the following combinations:

  • Single and Multiple Inheritance (not supported but can be achieved through interface)
  • Multilevel and Hierarchical Inheritance
  • Hierarchical and Single Inheritance
  • Multiple and Multilevel Inheritance

Here, we are going to take an example to demonstrate the working of hybrid inheritance in Java.

Example

Output:

D

FAQs about Inheritance

1. Why multiple inheritance is not supported in Java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in Java.

Suppose there are three classes A, B, and C. The C class inherits A and B classes. If A and B classes have the same method and we call it from child class object, there will be ambiguity to call the method of A or B class. It is called diamond problem.

To read more Diamond Problem in Java

Since compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So, whether you have same method or different, there will be compile time error.

Output:

 Compile Time Error

2. How to achieve Multiple Inheritance in Java?

Java supports multiple inheritance through interfaces only, where a class can implement multiple interfaces. Multiple inheritance in Java is not possible by class, but it is possible through interfaces.