Java – implements class methods inherited from two interfaces with different return types

I have two interfaces and a class that implements them:

interface A {
    int test();
}

interface B {
    void test();
}

class X implements A,B {
    public void test()  { // ==> Error

    }

    public int test() { // ==> Error
        return 10;
    }
}

Why can't I implement the method "test" in my x class?

Note: this situation cannot be corrected by design or logic, but I want to know what I should do in this case, because I am learning the Java language, and I want to know this situation:)

Solution

The return type is not in the method signature Method signatures include:

>Method name > format parameters > input parameters (if any)

Because the two methods have the same name and parameters, they have the same signature And you cannot use two methods with the same signature in a class

You can only solve this problem by changing 3 things about this method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>