Does Java support Multiple inheritance

Some languages (like C++) allow a class to extend more than one other classes. This capability is known as "multiple inheritance." 
Java doesn't support multiple inheritance because to avoid ambiguity problem ("deadly diamond of death" problem) and complexity of multiple inheritance. 
The diamond of death problem - when two classes B and C inherit from A, and class D inherits from both B and C. If D calls a method defined in A , and B and C have overridden that method differently, then from which class does it inherit: B, or C? Lets see the Daimon of death problem with an example :
class A {
 void show(){
 System.out.println("A");
 }
}
B and C have overridden the show() method differently;
 
class B extends A{
 void show(){
 System.out.println("B");
 }
}

class C extends A {
 void show(){
 System.out.println("C");
 }
}
lets assume Java supports multiple inheritance and class D extends both B and C class
 
class D extends B, C {
}
class Test{
 public static void main(String[] args) {
  D d = new D();
  d.show();  // which class show method will call? (B or C class)
 }
}
if you call show() in Test class, does it call B class show() or C class show()? the compiler does't know which method to call. Hope you understand the problem.
Then how other languages are supporting "multiple inheritance"? . Check wikipedia link for good explanation on how other langauages supporting multiple inheritances.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...