Why Java Does Not Support Multiple Inheritance?

In this tutorial you will learn why java does not support multiple inheritance. It is a very popular and frequently asked java interview question.

What is Multiple Inheritance?

When a class inherits two or more classes then this scenario is called as multiple inheritance.

Also Read: Why Java Does Not Support Pointers?

Why Java Does Not Support Multiple Inheritance?

Lets consider a situation to understand this. There are three classes A, B and C. A and B class contains a method display(). Class C inherits these two classes. In this case class C will be confused that which display() to inherit because both A and B contains display() method. To remove this kind of ambiguity java doesn’t support multiple inheritance.

Why Java Does Not Support Multiple Inheritance?

class A {
	void display(){
		//some statements
	}
}

class B {
	void display(){
		//some statements
	}
}

class C extends A, B{
	//inherit A's display() or B's display()?
}

 

How to Achieve Multiple Inheritance?

We can achieve multiple inheritance in java using interfaces. Consider below example.

interface A {
	public void display();
}

interface B {
	public void display();
}

class C implements A, B {
	public void display(){
		//some statements
	}
}

 

There is no ambiguity in above case because interface do not provides method body and the class that implements the interface has to provide method body. Class C will provide its own implementation of method display().

Leave a Comment

Your email address will not be published. Required fields are marked *