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.
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?
}
}
compile time error
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.