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.
Example:
Parent.java
- class Parent{
- int x;
- int y;
- void show(){
- System.out.println("Parent");
- }
- }
class Parent{ int x; int y; void show(){ System.out.println("Parent"); } }
Child.java
- class Child extends Parent{
- int z;
- void display(){
- System.out.println("Child");
- }
- }
class Child extends Parent{ int z; void display(){ System.out.println("Child"); } }
Manager.java
- 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
- }
- }
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.
Example:
Parent.java
- class Parent{
- int x;
- int y;
- void show(){
- System.out.println("Parent");
- }
- }
class Parent{ int x; int y; void show(){ System.out.println("Parent"); } }
Child1.java
- class Child1 extends Parent{
- int z;
- void display(){
- System.out.println("Child1");
- }
- }
class Child1 extends Parent{ int z; void display(){ System.out.println("Child1"); } }
Child2.java
- class Child2 extends Child1{
- int p;
- void print(){
- System.out.println("Child2");
- }
- }
class Child2 extends Child1{ int p; void print(){ System.out.println("Child2"); } }
Manager.java
- 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
- }
- }
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.
Example:
Parent.java
- class Parent{
- int x;
- int y;
- void show(){
- System.out.println("Parent");
- }
- }
class Parent{ int x; int y; void show(){ System.out.println("Parent"); } }
Child1.java
- class Child1 extends Parent{
- int z;
- void display(){
- System.out.println("Child1");
- }
- }
class Child1 extends Parent{ int z; void display(){ System.out.println("Child1"); } }
Child2.java
- class Child2 extends Parent{
- int p;
- void print(){
- System.out.println("Child2");
- }
- }
class Child2 extends Parent{ int p; void print(){ System.out.println("Child2"); } }
Manager.java
- 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
- }
- }
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.
Example:
Parent1.java
- interface Parent1{
- void show();
- }
interface Parent1{ void show(); }
Parent2.java
- interface Parent2 extends Parent1
- {
- void display();
- }
interface Parent2 extends Parent1 { void display(); }
Parent3.java
- interface Parent3 extends Parent1
- {
- void print();
- }
interface Parent3 extends Parent1 { void print(); }
Child.java
- class Child implements Parent2,Parent3{
- void show(){
- System.out.println("show");
- }
- void display(){
- System.out.println("display");
- }
- void print(){
- System.out.println("print");
- }
- }
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
- 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();
- }
- }
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.
Example:
Parent1.java
- interface Parent1{
- void show();
- }
interface Parent1{ void show(); }
Parent2.java
- interface Parent2
- {
- void display();
- }
interface Parent2 { void display(); }
Child.java
- class Child implements Parent1,Parent2{
- void show(){
- System.out.println("show");
- }
- void display(){
- System.out.println("display");
- }
- }
class Child implements Parent1,Parent2{ void show(){ System.out.println("show"); } void display(){ System.out.println("display"); } }
Manager.java
- 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();
- }
- }
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
- class Parent1{
- void show(){
- System.out.println("Parent1");
- }
- }
class Parent1{ void show(){ System.out.println("Parent1"); } }
Parent2.java
- class Parent2{
- void show(){
- System.out.println("Parent2");
- }
- }
class Parent2{ void show(){ System.out.println("Parent2"); } }
Child.java
- class Child extends Parent1,Parent2{
- //If it would have been allowed
- }
class Child extends Parent1,Parent2{ //If it would have been allowed }
Manager.java
- 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
- }
- }
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
- interface Parent1{
- void show();
- }
interface Parent1{ void show(); }
Parent2.java
- interface Parent2
- {
- void show();
- }
interface Parent2 { void show(); }
Child.java
- class Child implements Parent1,Parent2{
- void show(){
- System.out.println("Child");
- }
- }
class Child implements Parent1,Parent2{ void show(){ System.out.println("Child"); } }
Manager.java
- class Manager{
- public static void main(String args[]){
- Child c = new Child();
- c.show();// No ambiguity
- }
- }
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.
Amazing explanation.