Java – Types of Inheritance4 min read

Java supports three types of inheritance on the basis of class: single, multilevel, and hierarchical.

Whereas multiple and hybrid inheritance is supported through interface only.

Types of Inheritance in Java:

  • Single Inheritance
  • Multilevel Inheritance.
  • Hierarchical Inheritance.
  • Multiple Inheritance.
  • Hybrid Inheritance.

1. Single Inheritance:

It is a child and parent class relationship where a child class extends or inherits only from one class or superclass.
In the below diagram, class B extends only one class which is A. Here class A is a parent class of B and B would be a child class of A.

Single Inheritance

Example of Single Inheritance in Java:

Output of single Inheritance:

Super class method
Base class method


2. Multilevel Inheritance:

It is a child and parent class relationship where one can inherit from a derived class, thereby making this derived class the base class for the new class.
In the below diagram, class C is a subclass or child class of class B and class B is a child class of class A.

Multilevel Inheritance

Example of Multilevel Inheritance in Java:

Output of Multilevel Inheritance:

I am First Class
I am Second Class
I am Third Class


3. Hierarchical Inheritance:

In this type of inheritance, one superclass(base class) is inherited by many subclasses (derived class). That is inheriting the same class by many classes.
In the below diagram class B and class, C inherits the same class A. Class A is parent class (or base class) of class B and class C.

Hierarchical Inheritance

Example of Hierarchical Inheritance in Java:

Output of Hierarchical Inheritance:

I am dog
I am Horse and I run
I am dog
I am Cat and I sleep


4. Multiple Inheritance:

In this relationship, one class(derived class) can inherit from more than one superclass. Java doesn’t support multiple inheritance because the derived class will have to manage the dependency on two base classes.

  • In java, we can achieve multiple inheritance only through Interfaces.
  • It is rarely used because it creates an unwanted problem in the hierarchy.

5. Hybrid Inheritance:

It is the relationship form by the combination of multiple inheritance.
In a simple way, Hybrid inheritance is a combination of Single and Multiple inheritance.
It is also not supported in Java and can only be achieved through interface just like Multiple Inheritance.

Multiple and Hybrid Inheritance diagram:

Inheritance
Supported through Interfaces

MORE

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More