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

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

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

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

    For example two class A,B having same method m1(). And class C extends both A, B.

     class C extends A, B // for explaining purpose.
    

    Now, class C will search the definition of m1. First, it will search in class if it didn't find then it will check to parents class. Both A, B having the definition So here ambiguity occur which definition should choose. So JAVA DOESN'T SUPPORT MULTIPLE INHERITANCE.

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

    Because inheritance is overused even when you can't say "hey, that method looks useful, I'll extend that class as well".

    public class MyGodClass extends AppDomainObject, HttpServlet, MouseAdapter, 
                 AbstractTableModel, AbstractListModel, AbstractList, AbstractMap, ...
    
    0 讨论(0)
  • 2020-11-22 15:34

    Since this topic is not close I'll post this answer, I hope this helps someone to understand why java does not allow multiple inheritance.

    Consider the following class:

    public class Abc{
    
        public void doSomething(){
    
        }
    
    }
    

    In this case the class Abc does not extends nothing right? Not so fast, this class implicit extends the class Object, base class that allow everything work in java. Everything is an object.

    If you try to use the class above you'll see that your IDE allow you to use methods like: equals(Object o), toString(), etc, but you didn't declare those methods, they came from the base class Object

    You could try:

    public class Abc extends String{
    
        public void doSomething(){
    
        }
    
    }
    

    This is fine, because your class will not implicit extends Object but will extends String because you said it. Consider the following change:

    public class Abc{
    
        public void doSomething(){
    
        }
    
        @Override
        public String toString(){
            return "hello";
        }
    
    }
    

    Now your class will always return "hello" if you call toString().

    Now imagine the following class:

    public class Flyer{
    
        public void makeFly(){
    
        }
    
    }
    
    public class Bird extends Abc, Flyer{
    
        public void doAnotherThing(){
    
        }
    
    }
    

    Again class Flyer implicit extends Object which has the method toString(), any class will have this method since they all extends Object indirectly, so, if you call toString() from Bird, which toString() java would have to use? From Abc or Flyer? This will happen with any class that try to extends two or more classes, to avoid this kind of "method collision" they built the idea of interface, basically you could think them as an abstract class that does not extends Object indirectly. Since they are abstract they will have to be implemented by a class, which is an object (you cannot instanciate an interface alone, they must be implemented by a class), so everything will continue to work fine.

    To differ classes from interfaces, the keyword implements was reserved just for interfaces.

    You could implement any interface you like in the same class since they does not extends anything by default (but you could create a interface that extends another interface, but again, the "father" interface would not extends Object"), so an interface is just an interface and they will not suffer from "methods signature colissions", if they do the compiler will throw a warning to you and you will just have to change the method signature to fix it (signature = method name + params + return type).

    public interface Flyer{
    
        public void makeFly(); // <- method without implementation
    
    }
    
    public class Bird extends Abc implements Flyer{
    
        public void doAnotherThing(){
    
        }
    
        @Override
        public void makeFly(){ // <- implementation of Flyer interface
    
        }
    
        // Flyer does not have toString() method or any method from class Object, 
        // no method signature collision will happen here
    
    }
    
    0 讨论(0)
  • 2020-11-22 15:34

    Take for example the case where Class A has a getSomething method and class B has a getSomething method and class C extends A and B. What would happen if someone called C.getSomething? There is no way to determine which method to call.

    Interfaces basically just specify what methods a implementing class needs to contain. A class that implements multiple interfaces just means that class has to implement the methods from all those interfaces. Whci would not lead to any issues as described above.

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

    * This is a simple answer since I'm a beginner in Java *

    Consider there are three classes X,Y and Z.

    So we are inheriting like X extends Y, Z And both Y and Z is having a method alphabet() with same return type and arguments. This method alphabet() in Y says to display first alphabet and method alphabet in Z says display last alphabet. So here comes ambiguity when alphabet() is called by X. Whether it says to display first or last alphabet??? So java is not supporting multiple inheritance. In case of Interfaces, consider Y and Z as interfaces. So both will contain the declaration of method alphabet() but not the definition. It won't tell whether to display first alphabet or last alphabet or anything but just will declare a method alphabet(). So there is no reason to raise the ambiguity. We can define the method with anything we want inside class X.

    So in a word, in Interfaces definition is done after implementation so no confusion.

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

    Because interfaces specify only what the class is doing, not how it is doing it.

    The problem with multiple inheritance is that two classes may define different ways of doing the same thing, and the subclass can't choose which one to pick.

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