覆盖子接口中接口的方法/方法的原因是什么?

sub*_*his 1 java ooad

覆盖子接口中接口的方法/方法的原因是什么?

例如

interface I{ public void method();}
interface I2 extends I{@Override public void method();}
Run Code Online (Sandbox Code Playgroud)

Meh*_*van 5

您可能需要将方法的返回类型更改为原始返回类型的子类型.例如:

interface I {
    public Object method();
}

interface I2 extends I {
    @Override
    public Integer method();
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以default向Java 8中引入的方法添加实现.例如:

interface I {
    public void method();
}

interface I2 extends I {
    @Override
    default public void method() {
        System.out.println("do something");
    }
}
Run Code Online (Sandbox Code Playgroud)