+1 vote
in JAVA by
Why is multiple inheritance not supported in java?

1 Answer

0 votes
by
To reduce the complexity and simplify the language, multiple inheritance is not supported in java. Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class.

Since the compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes. So whether you have the same method or different, there will be a compile time error.

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?  

}  

}  

Test it Now

 Compile Time Error

Related questions

+1 vote
asked Dec 7, 2020 in JAVA by SakshiSharma
0 votes
asked May 25, 2020 in JAVA by Robindeniel
...