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 understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.