1.

Why multiple inheritance is not supported in Java?

Answer»

A key part of object-oriented PROGRAMMING is MULTIPLE inheritance. This means that a class inherits the properties of multiple classes.

However, multiple inheritance may lead to many problems. Some of these are:

  1. The diamond problem occurs when 2 classes (B and C) inherit from a single class(A) and then a class(D) inherits these 2 classes(B and C) using multiple inheritance. This leads to multiple copies of class members of class A in class D which can create problems.
  2. The ambiguity problem occurs if a class inherits from 2 classes that contain the same NAME for a class member. Then there is ambiguity in the inherited class when that class member is required.

The above problems are one of the reasons that Java doesn't support multiple inheritance.

A PROGRAM that demonstrates the diamond problem in Java is given as follows:

PUBLIC class A {    void display()    {        System.out.println("This is class A");    } } public class B extends A {    void display()      {        System.out.println("This is class B");    } } public class C extends A {    void display()    {        System.out.println("This is class C");    } } public class D extends B, C {   public static void main(String args[])   {       D obj = new D();       D.display();   } }

The above program leads to an error as multiple inheritance is not allowed Java.



Discussion

No Comment Found