Why java doesn't support multiple inheritance?

 
Why Java doesn’t support Multiple Inheritance?
 
Consider the below Java code. It shows error.
 
// First Parent class
class Parent1
{
	void fun()
	{
		System.out.println("Parent1");
	}
}

// Second Parent Class
class Parent2
{
	void fun()
	{
		System.out.println("Parent2");
	}
}

// Error : Test is inheriting from multiple
// classes
class Test extends Parent1, Parent2
{
public static void main(String args[])
{
	Test t = new Test();
	t.fun();
}
}

Output :

Compiler Error

From the code, we see that, on calling the tactic fun() using Test object will cause complications like whether to call Parent1’s fun() or Parent2’s fun() method.

1. The Diamond Problem:

The below Java program throws compiler error when run. Multiple inheritance causes diamond problem when allowed in other languages like C++.

 

      GrandParent
           /     \
          /       \
      Parent1      Parent2
          \       /
           \     /
             Test

 

// A Grand parent class in diamond
class GrandParent
{
	void fun()
	{
		System.out.println("Grandparent");
	}
}

// First Parent class
class Parent1 extends GrandParent
{
	void fun()
	{
		System.out.println("Parent1");
	}
}

// Second Parent Class
class Parent2 extends GrandParent
{
	void fun()
	{
		System.out.println("Parent2");
	}
}


// Error : Test is inheriting from multiple
// classes
class Test extends Parent1, Parent2
{
public static void main(String args[])
{
	Test t = new Test();
	t.fun();
}
}

Output :

Error : 
prog.java:31: error: '{' expected
class Test extends Parent1, Parent2 
                          ^
1 error

From the code, we see that: On calling the tactic fun() using Test object will cause complications like whether to call Parent1’s fun() or Child’s fun() method.

Therefore, so as to avoid such complications Java doesn't support multiple inheritance of classes.

2. Simplicity – Multiple inheritance isn't supported by Java using classes , handling the complexity that causes thanks to multiple inheritance is extremely complex. It creates problem during various operations like casting, constructor chaining etc and therefore the in particular reason is that there are only a few scenarios on which we really need multiple inheritance, so better to omit it for keeping the items simple and easy .

Java 8 supports default methods where interfaces can provide default implementation of methods. And a category can implement two or more interfaces. just in case both the implemented interfaces contain default methods with same method signature, the implementing class should explicitly specify which default method is to be used or it should override the default method.

 

// A simple Java program to demonstrate multiple
// inheritance through default methods.
interface PI1
{
	// default method
	default void show()
	{
		System.out.println("Default PI1");
	}
}

interface PI2
{
	// Default method
	default void show()
	{
		System.out.println("Default PI2");
	}
}

// Implementation class code
class TestClass implements PI1, PI2
{
	// Overriding default show method
	public void show()
	{
		// use super keyword to call the show
		// method of PI1 interface
		PI1.super.show();

		// use super keyword to call the show
		// method of PI2 interface
		PI2.super.show();
	}

	public static void main(String args[])
	{
		TestClass d = new TestClass();
		d.show();
	}
}

Output:

Default PI1
Default PI2

If we remove implementation of default method from “TestClass”, we get compiler error. See this for a sample run.

If there's a diamond through interfaces, then there's no issue if none of the center interfaces provide implementation of root interface. If they supply implementation, then implementation are often accessed as above using super keyword.

 

// A simple Java program to demonstrate how diamond
// problem is handled in case of default methods

interface GPI
{
	// default method
	default void show()
	{
		System.out.println("Default GPI");
	}
}

interface PI1 extends GPI { }

interface PI2 extends GPI { }

// Implementation class code
class TestClass implements PI1, PI2
{
	public static void main(String args[])
	{
		TestClass d = new TestClass();
		d.show();
	}
}

Output:

Default GPI

 

Submit Your Programming Assignment Details