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

81 views Asked by At

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();}
1

There are 1 answers

1
Mehdi Javan On

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");
    }
}