Javatpoint Logo
Javatpoint Logo

Types of Inheritance in Java

Inheritance is the most powerful feature of object-oriented programming. It allows us to inherit the properties of one class into another class. In this section, we will discuss types of inheritance in Java in-depth with real-life examples. Also, we will create Java programs to implement the concept of different types of inheritance.

Inheritance

Inheritance is a mechanism of driving a new class from an existing class. The existing (old) class is known as base class or super class or parent class. The new class is known as a derived class or sub class or child class. It allows us to use the properties and behavior of one class (parent) in another class (child).

A class whose properties are inherited is known as parent class and a class that inherits the properties of the parent class is known as child class. Thus, it establishes a relationship between parent and child class that is known as parent-child or Is-a relationship.

Suppose, there are two classes named Father and Child and we want to inherit the properties of the Father class in the Child class. We can achieve this by using the extends keyword.


Types of Inheritance in Java

When we should use inheritance?

Inheritance provides the reusability of code especially when there is a large scale of code to reuse. It also establishes the relationship between different classes that is known as a Is-a relationship. We can also use it if we want to achieve method overriding.

Points to Remember

  • Constructor cannot be inherited in Java.
  • Private members do not get inherited in Java.
  • Cyclic inheritance is not permitted in Java.
  • Assign parent reference to child objects.
  • Constructors get executed because of super() present in the constructor.

Types of Inheritance

Java supports the following four types of inheritance:

  • Single Inheritance
  • Multi-level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
Types of Inheritance in Java

Note: Multiple inheritance is not supported in Java.

Let's discuss each with proper example.

Single Inheritance

In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes it is also known as simple inheritance.

Types of Inheritance in Java

In the above figure, Employee is a parent class and Executive is a child class. The Executive class inherits all the properties of the Employee class.

Let's implement the single inheritance mechanism in a Java program.

Executive.java

Output:

Total salary credited: 414408.0
Bonus of six months: 18000.0

Multi-level Inheritance

In multi-level inheritance, a class is derived from a class which is also derived from another class is called multi-level inheritance. In simple words, we can say that a class that has more than one parent class is called multi-level inheritance. Note that the classes must be at different levels. Hence, there exists a single base class and single derived class but multiple intermediate base classes.

Types of Inheritance in Java

In the above figure, the class Marks inherits the members or methods of the class Students. The class Sports inherits the members of the class Marks. Therefore, the Student class is the parent class of the class Marks and the class Marks is the parent of the class Sports. Hence, the class Sports implicitly inherits the properties of the Student along with the class Marks.

Let's implement the multi-level inheritance mechanism in a Java program.

MultilevelInheritanceExample.java

Output:

registration number= 0987
marks= 78.0
score= 68.7

Hierarchical Inheritance

If a number of classes are derived from a single base class, it is called hierarchical inheritance.

Types of Inheritance in Java

In the above figure, the classes Science, Commerce, and Arts inherit a single parent class named Student.

Let's implement the hierarchical inheritance mechanism in a Java program.

HierarchicalInheritanceExample.java

Output:

The method of the class Student invoked.
The method of the class Student invoked.
The method of the class Student invoked.

Hybrid Inheritance

Hybrid means consist of more than one. Hybrid inheritance is the combination of two or more types of inheritance.

Types of Inheritance in Java

In the above figure, GrandFather is a super class. The Father class inherits the properties of the GrandFather class. Since Father and GrandFather represents single inheritance. Further, the Father class is inherited by the Son and Daughter class. Thus, the Father becomes the parent class for Son and Daughter. These classes represent the hierarchical inheritance. Combinedly, it denotes the hybrid inheritance.

Let's implement the hybrid inheritance mechanism in a Java program.

Daughter.java

Output:

I am daughter.

Multiple Inheritance (not supported)

Java does not support multiple inheritances due to ambiguity. For example, consider the following Java program.

Demo.java

The above code gives error because the compiler cannot decide which message() method is to be invoked. Due to this reason, Java does not support multiple inheritances at the class level but can be achieved through an interface.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA