subhashis
subhashis

Reputation: 4878

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

Upvotes: 1

Views: 80

Answers (1)

Mehdi Javan
Mehdi Javan

Reputation: 1091

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

Upvotes: 5

Related Questions