Types of Inheritance


Let us understand various types of inheritance with their example.


There are various types of inheritance supported in Java and these types are defined based on the hierarchical structure of classes and they are as below

1) Single level inheritance

2) Multi level inheritance

3) Hierarchical inheritance

4) Hybrid inheritance
(Supported only using interface)

5) Multiple inheritance
(Supported only using interface)


1) single level Inheritance


When one class extends only one parent class then it is called as Single level inheritance.

It means it will have only one parent class and only one subclass.

We can see the same in the below diagram.

single_level

Example:

Parent.java

  1. class Parent{
  2. int x;
  3. int y;
  4. void show(){
  5. System.out.println("Parent");
  6. }
  7. }
class Parent{
int x;
int y;
void show(){
System.out.println("Parent");
}
}


Child.java

  1. class Child extends Parent{
  2. int z;
  3.  
  4. void display(){
  5. System.out.println("Child");
  6. }
  7. }
class Child extends Parent{
int z;

void display(){
System.out.println("Child");
}
}


Manager.java

  1. class Manager{
  2. public static void main(String args[]){
  3. Child c = new Child();
  4. c.show(); //Calling super class method as it is inherited and made available inside Child class also
  5. c.display(); //Calling child class specific method
  6.  
  7. }
  8. }
class Manager{
public static void main(String args[]){
Child c = new Child();
c.show(); //Calling super class method as it is inherited and made available inside Child class also
c.display(); //Calling child class specific method

}
}




2) Multi level Inheritance


In this case, one child class extends parent class and that child class can become the parent class for the new child class.

It means it will have multiple level in the <code>super and subclass relation.

We can see the same in the below diagram.

multilevel_Inheritance

Example:

Parent.java

  1. class Parent{
  2. int x;
  3. int y;
  4. void show(){
  5. System.out.println("Parent");
  6. }
  7. }
class Parent{
int x;
int y;
void show(){
System.out.println("Parent");
}
}


Child1.java

  1. class Child1 extends Parent{
  2. int z;
  3.  
  4. void display(){
  5. System.out.println("Child1");
  6. }
  7. }
class Child1 extends Parent{
int z;

void display(){
System.out.println("Child1");
}
}


Child2.java

  1. class Child2 extends Child1{
  2. int p;
  3.  
  4. void print(){
  5. System.out.println("Child2");
  6. }
  7. }
class Child2 extends Child1{
int p;

void print(){
System.out.println("Child2");
}
}


Manager.java

  1. class Manager{
  2. public static void main(String args[]){
  3. Child2 c = new Child2();
  4. c.show(); //Calling Parent class method as it is inherited from Parent class
  5. c.display(); //Calling Child1 class method as its inherited from Child1 class
  6. c.print();//Calling Child2 class specific method
  7.  
  8. }
  9. }
class Manager{
public static void main(String args[]){
Child2 c = new Child2();
c.show(); //Calling Parent class method as it is inherited from Parent class
c.display(); //Calling Child1 class method as its inherited from Child1 class
c.print();//Calling Child2 class specific method

}
}




3) Hierarchical Inheritance


In this case, one parent class is inherited by multiple sub classes.

It means all the sub classes will have a common Super class.

We can see the same in the below diagram.

hierarchical_Inheritance

Example:

Parent.java

  1. class Parent{
  2. int x;
  3. int y;
  4. void show(){
  5. System.out.println("Parent");
  6. }
  7. }
class Parent{
int x;
int y;
void show(){
System.out.println("Parent");
}
}


Child1.java

  1. class Child1 extends Parent{
  2. int z;
  3.  
  4. void display(){
  5. System.out.println("Child1");
  6. }
  7. }
class Child1 extends Parent{
int z;

void display(){
System.out.println("Child1");
}
}


Child2.java

  1. class Child2 extends Parent{
  2. int p;
  3.  
  4. void print(){
  5. System.out.println("Child2");
  6. }
  7. }
class Child2 extends Parent{
int p;

void print(){
System.out.println("Child2");
}
}


Manager.java

  1. class Manager{
  2. public static void main(String args[]){
  3.  
  4. Child1 c1 = new Child1();
  5. c1.show(); //Calling Parent class method as it is inherited from Parent class
  6. c1.display(); //Calling Child1 class specific method
  7.  
  8. Child2 c2 = new Child2();
  9. c2.show(); //Calling Parent class method as it is inherited from Parent class
  10. c2.print();//Calling Child2 class specific method
  11. }
  12. }
class Manager{
public static void main(String args[]){

Child1 c1 = new Child1();
c1.show(); //Calling Parent class method as it is inherited from Parent class
c1.display(); //Calling Child1 class specific method

Child2 c2 = new Child2();
c2.show(); //Calling Parent class method as it is inherited from Parent class
c2.print();//Calling Child2 class specific method
}
}




4) Hybrid Inheritance


Its a combination of single and multiple inheritance.

In this case,multiple sub classes will have one super class and another class will have those sub classes as parent classes.

Its also called Diamond shape inheritance as its structure looks like a Diamond.

It can be achieved in Java using Interface only as Multiple inheritance is not supported in Java through classes.

We can see the same in the below diagram.

hybrid_Inheritance

Example:


Parent1.java

  1. interface Parent1{
  2. void show();
  3. }
interface Parent1{
void show();
}


Parent2.java

  1. interface Parent2 extends Parent1
  2. {
  3. void display();
  4. }
interface Parent2 extends Parent1
{
void display();
}


Parent3.java

  1. interface Parent3 extends Parent1
  2. {
  3. void print();
  4. }
interface Parent3 extends Parent1
{
void print();
}


Child.java

  1. class Child implements Parent2,Parent3{
  2. void show(){
  3. System.out.println("show");
  4. }
  5. void display(){
  6. System.out.println("display");
  7. }
  8. void print(){
  9. System.out.println("print");
  10. }
  11. }
class Child implements Parent2,Parent3{
void show(){
System.out.println("show");
}
void display(){
System.out.println("display");
}
void print(){
System.out.println("print");
}
}


Manager.java

  1. class Manager{
  2. public static void main(String args[]){
  3.  
  4. Child c = new Child();
  5.  
  6. //Calling all methods inherited and implemented in sub class.
  7. c.show();
  8. c.display();
  9. c.print();
  10.  
  11. }
  12. }
class Manager{
public static void main(String args[]){

Child c = new Child();

//Calling all methods inherited and implemented in sub class.
c.show();
c.display(); 
c.print();

}
}





5) Multiple Inheritance


In this case,sub class will extend multiple super classes or one interface extend more than one interface.

In Java, its not supported through class but it is supported through interface.

We can see the same in the below diagram.

multiple_Inheritance

Example:


Parent1.java

  1. interface Parent1{
  2. void show();
  3. }
interface Parent1{
void show();
}


Parent2.java

  1. interface Parent2
  2. {
  3. void display();
  4. }
interface Parent2
{
void display();
}


Child.java

  1. class Child implements Parent1,Parent2{
  2. void show(){
  3. System.out.println("show");
  4. }
  5. void display(){
  6. System.out.println("display");
  7. }
  8.  
  9. }
class Child implements Parent1,Parent2{
void show(){
System.out.println("show");
}
void display(){
System.out.println("display");
}

}


Manager.java

  1. class Manager{
  2. public static void main(String args[]){
  3.  
  4. Child c = new Child();
  5.  
  6. //Calling all methods inherited and implemented in sub class.
  7. c.show();
  8. c.display();
  9. }
  10. }
class Manager{
public static void main(String args[]){

Child c = new Child();

//Calling all methods inherited and implemented in sub class.
c.show();
c.display(); 
}
}




Why multiple inheritance is not supported in Java ?


Most of the object oriented languages like Java, c# do not support multiple inheritance as it involves lot of complexity and also it is very rarely used.

Multiple inheritance through classes can cause ambiguity on inheriting method definition from parent classes if all of them have same method.

Consider the below scenario
Parent1 , Parent2 and Child are the 3 classes and “Child” class has to inherit from both Parent1 and Parent2 classes.

Parent1.java

  1. class Parent1{
  2. void show(){
  3. System.out.println("Parent1");
  4. }
  5. }
class Parent1{
void show(){
System.out.println("Parent1");
}
}


Parent2.java

  1. class Parent2{
  2. void show(){
  3. System.out.println("Parent2");
  4. }
  5. }
class Parent2{
void show(){
System.out.println("Parent2");
}
}


Child.java

  1. class Child extends Parent1,Parent2{
  2.  //If it would have been allowed
  3. }
class Child extends Parent1,Parent2{
 //If it would have been allowed
}


Manager.java

  1. class Manager{
  2.  Public Static void main(String args[]){
  3. Child c = new Child();
  4. c.show(); //Which "show()" method (Parent1 or Parent2) has to be inherited and invoked - ambiguity
  5. }
  6. }
class Manager{
 Public Static void main(String args[]){ 
Child c = new Child();
c.show(); //Which "show()" method (Parent1 or Parent2) has to be inherited and invoked - ambiguity
}
}

Note:
If multiple inheritance would have been allowed through class then Child class will be in ambiguity as it does not know which method it has to inherit(whether one from Parent1 or from Parent2).

Why multiple inheritance is allowed through Interface ?


If we have interface then, class which is implementing multiple interface will be providing the implementation for the method declared in those interfaces and hence it will not have any ambiguity while calling the method.

Parent1.java

  1. interface Parent1{
  2. void show();
  3. }
interface Parent1{
void show();
}


Parent2.java

  1. interface Parent2
  2. {
  3. void show();
  4. }
interface Parent2
{
void show();
}


Child.java

  1. class Child implements Parent1,Parent2{
  2. void show(){
  3. System.out.println("Child");
  4. }
  5. }
class Child implements Parent1,Parent2{
void show(){
System.out.println("Child");
}
}


Manager.java

  1. class Manager{
  2. public static void main(String args[]){
  3. Child c = new Child();
  4.  
  5. c.show();// No ambiguity
  6.  
  7. }
  8. }
class Manager{
public static void main(String args[]){
Child c = new Child();

c.show();// No ambiguity

}
}


There is no ambiguity as it calls the method from “Child” class since its implementation is available only in the Child class.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on