Home » Java programming language

Why java does not support multiple inheritances?



In this article, we are going to learn about one of the java’s inheritance features - why it does not support multiple inheritances?
Submitted by Preeti Jain, on February 18, 2018

First we will understand what is inheritance?

Inheritance means when a method is defined once and it can use anywhere without rewriting again. With the help of inheritance. We can achieve reusability.

What is multiple inheritance?

Inheritance is a concept which is applicable in most of the programming language. When class A is extendable by class B and a class B is extendable by class C.

Syntax

interface A{

	public void a(){

	}
}

interface B extends A{

	public void a(){

	}
}

class C implements A,B{

	public void c(){

	}
}

In the above syntax when we will made an object of C class then with help of C class object if we call a() method with C class object then we will get ambiguity.

Java resolves ambiguity problem with the help of interface.

We cannot achieve multiple inheritance with the help of interface because interface methods need to be redefined in every child class but inheritance don't need to be redefined again that's why we cannot say interface behaves like multiple inheritance.

Example

interface A{
	public void a();
}

interface B extends A{
	public void a();
}

class InterfaceAB implements A,B{
	public void a(){
		System.out.println("Interface A a()");
	}

	public static void main(String[] args){
		InterfaceAB iab = new InterfaceAB();
		iab.a();
	}
}

Output

D:\Java Articles>java InterfaceAB
Interface A a()





Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.



Comments and Discussions





© https://www.includehelp.com (2015-2018), Some rights reserved.




close Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.