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

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

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

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

    Because an interface is just a contract. And a class is actually a container for data.

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

    Consider a scenario where Test1, Test2 and Test3 are three classes. The Test3 class inherits Test2 and Test1 classes. If Test1 and Test2 classes have same method and you call it from child class object, there will be ambiguity to call method of Test1 or Test2 class but there is no such ambiguity for interface as in interface no implementation is there.

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

    Java does not support multiple inheritance , multipath and hybrid inheritance because of ambiguity problem:

     Scenario for multiple inheritance: Let us take class A , class B , class C. class A has alphabet(); method , class B has also alphabet(); method. Now class C extends A, B and we are creating object to the subclass i.e., class C , so  C ob = new C(); Then if you want call those methods ob.alphabet(); which class method takes ? is class A method or class B method ?  So in the JVM level ambiguity problem occurred. Thus Java does not support multiple inheritance.
    

    multiple inheritance

    Reference Link: https://plus.google.com/u/0/communities/102217496457095083679

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

    One of my college instructors explained it to me this way:

    Suppose I have one class, which is a Toaster, and another class, which is NuclearBomb. They both might have a "darkness" setting. They both have an on() method. (One has an off(), the other doesn't.) If I want to create a class that's a subclass of both of these...as you can see, this is a problem that could really blow up in my face here.

    So one of the main issues is that if you have two parent classes, they might have different implementations of the same feature — or possibly two different features with the same name, as in my instructor's example. Then you have to deal with deciding which one your subclass is going to use. There are ways of handling this, certainly — C++ does so — but the designers of Java felt that this would make things too complicated.

    With an interface, though, you're describing something the class is capable of doing, rather than borrowing another class's method of doing something. Multiple interfaces are much less likely to cause tricky conflicts that need to be resolved than are multiple parent classes.

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

    It is said that objects state is referred with respect to the fields in it and it would become ambiguous if too many classes were inherited. Here is the link

    http://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.html

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

    Java does not support multiple inheritance because of two reasons:

    1. In java, every class is a child of Object class. When it inherits from more than one super class, sub class gets the ambiguity to acquire the property of Object class..
    2. In java every class has a constructor, if we write it explicitly or not at all. The first statement is calling super() to invoke the supper class constructor. If the class has more than one super class, it gets confused.

    So when one class extends from more than one super class, we get compile time error.

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