简体   繁体   中英

What is the reason behind overriding a method/methods of an interface in the sub interface?

What is the reason behind overriding a method/methods of an interface in the sub interface?

for example

interface I{ public void method();}
interface I2 extends I{@Override public void method();}

You may need to change the return type of your method to a sub-type of the original return type. eg:

interface I {
    public Object method();
}

interface I2 extends I {
    @Override
    public Integer method();
}

Or you can add default implementation to the method which is introduced in Java 8. eg:

interface I {
    public void method();
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM