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

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

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

18条回答
  •  南笙
    南笙 (楼主)
    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
    
    }
    

提交回复
热议问题