Tuesday, 24 June 2014

Why java is not supporting multiple inheritance

- - First reason is ambiguity around Diamond problem occurs.

- - multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc 

- - Since interface only have method declaration and doesn't provide any implementation there will only be just one implementation of specific method hence there would not be any ambiguity.



Example :
   
  1. class A{  
  2. void msg(){System.out.println("Hello");}  
  3. }  
  4.   
  5. class B{  
  6. void msg(){System.out.println("Welcome");}  
  7. }  
  8.   
  9. class C extends A,B{//suppose if it were  
  10.    
  11.  Public Static void main(String args[]){  
  12.    C obj=new C();  
  13.    obj.msg();//Now which msg() method would be invoked?  
  14. }  
  15. }  

No comments:

Post a Comment