multiple inheritance is not supported in java

Java Programming Language / Class, Object and Methods in java

1066

Multiple inheritance is not supported in Java because it can lead to the diamond problem, which occurs when a class inherits from two classes, both of which have a common parent class. In such cases, the ambiguity arises as to which parent class method should be inherited by the child class.

To avoid this problem, Java uses interfaces, which provide a way to achieve multiple inheritance by allowing a class to implement multiple interfaces. This way, the ambiguity problem is avoided as interfaces only contain method signatures and the implementation of those methods is done by the implementing class.

Program:

 class A{
void msg(){
	System.out.println("Hello");
	}
}

class B{
void msg(){
	System.out.println("Welcome");
	}
}

class C extends A,B{//suppose if it were

 Public Static void main(String args[]){
   C obj=new C();
   obj.msg();//Now which msg() method would be invoked?
 }
}

Output:

compile time error

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.