group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
263 Members
Join this group to post and comment.
Gaurav Prasad
Gaurav Prasad
Computer Science
19 Jan 2019

Why Java doesn’t support multiple inheritance?

give one example stating it?

DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
3mos ago

Lets say we have a class C and in your main method you are creating an object of C. But when you call show() method through the object of C, but we don't have show() method. It will give a compile time error that we don't have show() method. Now consider there is one more class A and extend class C. Now if you have class B where class C extents class A and class B. If you have show() method in Class B. Then the show() method from Class B is called. But what if you have show() method in class A as well as in Class B. at this time there will be a real confusion which method to call. Such problem is called ambiguity problem where compiler cannot decide which method to call. To solve ambiguity problem Java doesn't support multiple inheritance. Using classes you cannot support multiple inheritance. But we can use multiple classes in directly.

The following is the example where there occurs ambiguity problem,

class C extends A,B{

}

main(){

c obj = new c();

obj.show();

}

class A{

show();

}

class B

{

show();

}