Multiple inheritance in Java Interface

We know that Java classes can only inherit a class, but can implement multiple interfaces. But you know what? Java Interface but you can inherit multiple interfaces. In this paper, to talk about multiple inheritance in Java interface.

Before entering the topic, to expand it. Why Java supports only single inheritance it?

We might assume that Java supports multiple inheritance, for example, where there is a class A, we write two classes Class B and Class C, and Class B and Class C are inherited class A and class with the A's a method of coverage. In this case again, if we write a class D, class D and to simultaneously integrate multiple inheritance class B and class C, class D will inherit then class B and class C overloaded methods from class A, as in FIG. Fig. So the question is, Class D began to be puzzled, I should in the end the method in which class which inherited it, since the class is structural, which would cause confusion on the structure. This is multiple inheritance diamond inheritance problem.

At the same time we know that C ++ supports multiple inheritance, because it solves the problem (I'm not familiar C ++, checked the information, seems to be achieved through the virtual base class, right). But Java spirit of simple principles, abandoning multiple inheritance.

Well, that is entered. We give you an example to demonstrate what multiple inheritance interface.

Swallow is a bird, a bird can fly, will sing. Let's imitate it:

First, the flying Interface

package multiex;

public interface Flyable {
    public void fly();
}

Second, the singing of the interface

package multiex;

public interface Singable  {
    public void sing();
}

Third, the birds of the interface
bird interface inherits the above two interfaces

package multiex;
//虽然这个接口没有定义方法,但是会继承下来两个方法
public interface Bird extends Flyable,Singable {

}

Fourth, like swallows, birds implement interfaces

package multiex;

//燕子类
public class Swallow implements Bird {

    @Override
    public void fly() {
        System.out.println("燕子会飞");
        
    }

    @Override
    public void sing() {
        System.out.println("燕子会唱歌");
    }
}

Fifth, the test class

package multiex;

public class Main {
    
    public static void main(String[] args) {
        Swallow swallow = new Swallow();
        swallow.fly();
        swallow.sing();
    }
}

operation result:

The above demonstrates the multiple-inheritance interfaces, then there is a problem here. If you have multiple interfaces in the same name of the method how to do? For example, as follows:

package multiex;

interface A {
    void m();
}
//注意:方法返回值不一样
interface B {
    int m();
}

class C implements A, B {
    public void m() {
        System.out.println("void m()");
    }

    public int m() {
        System.out.println("int m()");
    }
}

public class Test {
    public static void main(String[] args) {
        C c1 = new C();
        c1.m();
    }
}

In this case, the compiler will not pass. As shown below:

Because in Java,
a method parameter name + (excluding return type) to determine a unique method.
The method parameter name + (excluding return type) to determine a unique method.
The method parameter name + (excluding return type) to determine a unique method.

So when the return value is not the same time, Java believe this is the same method, which will return the type of conflict with an interface. It causes a compilation error.

Similarly, when the return value is the same, that this is entirely the same method to achieve a class implements this method just fine. As shown below:

how about it? Students did not you understand?

 



Author: small North seek
link: https: //www.jianshu.com/p/017f1a6d6fd9
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin blog.csdn.net/sinolover/article/details/95046123