Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

后端 未结 18 1773
囚心锁ツ
囚心锁ツ 2020-11-22 14:55

Java doesn\'t allow multiple inheritance, but it allows implementing multiple interfaces. Why?

相关标签:
18条回答
  • 2020-11-22 15:14

    For the same reason C# doesn't allow multiple inheritence but allows you to implement multiple interfaces.

    The lesson learned from C++ w/ multiple inheritence was that it lead to more issues than it was worth.

    An interface is a contract of things your class has to implement. You don't gain any functionality from the interface. Inheritence allows you to inherit the functionality of a parent class (and in multiple-inheritence, that can get extremely confusing).

    Allowing multiple interfaces allows you to use Design Patterns (like Adapter) to solve the same types of issues you can solve using multiple inheritence, but in a much more reliable and predictable manner.

    0 讨论(0)
  • 2020-11-22 15:14

    in simple manner we all know, we can inherit(extends) one class but we can implements so many interfaces.. that is because in interfaces we don't give an implementation just say the functionality. suppose if java can extends so many classes and those have same methods.. in this point if we try to invoke super class method in the sub class what method suppose to run??, compiler get confused example:- try to multiple extends but in interfaces those methods don't have bodies we should implement those in sub class.. try to multiple implements so no worries..

    0 讨论(0)
  • 2020-11-22 15:15

    The answer of this question is lies in the internal working of java compiler(constructor chaining). If we see the internal working of java compiler:

    public class Bank {
      public void printBankBalance(){
        System.out.println("10k");
      }
    }
    class SBI extends Bank{
     public void printBankBalance(){
        System.out.println("20k");
      }
    }
    

    After compiling this look like:

    public class Bank {
      public Bank(){
       super();
      }
      public void printBankBalance(){
        System.out.println("10k");
      }
    }
    class SBI extends Bank {
     SBI(){
       super();
     }
     public void printBankBalance(){
        System.out.println("20k");
      }
    }
    

    when we extends class and create an object of it, one constructor chain will run till Object class.

    Above code will run fine. but if we have another class called Car which extends Bank and one hybrid(multiple inheritance) class called SBICar:

    class Car extends Bank {
      Car() {
        super();
      }
      public void run(){
        System.out.println("99Km/h");
      }
    }
    class SBICar extends Bank, Car {
      SBICar() {
        super(); //NOTE: compile time ambiguity.
      }
      public void run() {
        System.out.println("99Km/h");
      }
      public void printBankBalance(){
        System.out.println("20k");
      }
    }
    

    In this case(SBICar) will fail to create constructor chain(compile time ambiguity).

    For interfaces this is allowed because we cannot create an object of it.

    For new concept of default and static method kindly refer default in interface.

    Hope this will solve your query. Thanks.

    0 讨论(0)
  • 2020-11-22 15:19

    Java supports multiple inheritance through interfaces only. A class can implement any number of interfaces but can extend only one class.

    Multiple inheritance is not supported because it leads to deadly diamond problem. However, it can be solved but it leads to complex system so multiple inheritance has been dropped by Java founders.

    In a white paper titled “Java: an Overview” by James Gosling in February 1995(link) gives an idea on why multiple inheritance is not supported in Java.

    According to Gosling:

    "JAVA omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. This primarily consists of operator overloading (although it does have method overloading), multiple inheritance, and extensive automatic coercions."

    0 讨论(0)
  • 2020-11-22 15:20

    Implementing multiple interfaces is very useful and doesn't cause much problems to language implementers nor programmers. So it is allowed. Multiple inheritance while also useful, can cause serious problems to users (dreaded diamond of death). And most things you do with multiple inheritance can be also done by composition or using inner classes. So multiple inheritance is forbidden as bringing more problems than gains.

    0 讨论(0)
  • 2020-11-22 15:23

    You can find accurate answer for this query in oracle documentation page about multiple inheritance

    1. Multiple inheritance of state: Ability to inherit fields from multiple classes

      One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes

      If multiple inheritance is allowed and When you create an object by instantiating that class, that object will inherit fields from all of the class's superclasses. It will cause two issues.

      1. What if methods or constructors from different super classes instantiate the same field?
      2. Which method or constructor will take precedence?
    2. Multiple inheritance of implementation: Ability to inherit method definitions from multiple classes

      Problems with this approach: name conflicts and ambiguity. If a subclass and superclass contain same method name (and signature), compiler can't determine which version to invoke.

      But java supports this type of multiple inheritance with default methods, which have been introduced since Java 8 release. The Java compiler provides some rules to determine which default method a particular class uses.

      Refer to below SE post for more details on resolving diamond problem:

      What are the differences between abstract classes and interfaces in Java 8?

    3. Multiple inheritance of type: Ability of a class to implement more than one interface.

      Since interface does not contain mutable fields, you do not have to worry about problems that result from multiple inheritance of state here.

    0 讨论(0)
提交回复
热议问题