Search code examples
javainheritancemethod-hiding

Inherit method with unrelated return types


I have the following code snippet

public class Test {
    static interface I1 { I1 m(); }

    static interface I2 { I2 m(); }

    static interface I12 extends I1,I2 { I12 m(); }

    public static void main(String [] args) throws Exception {
    }
}

When I try to compile it I got error.

Test.java:12: types Test.I2 and Test.I1 are incompatible; both define m(), but with unrelated return types.

How to avoid this?


Solution

  • As discussed in Java - Method name collision in interface implementation you can't do this.

    As a workaround, you can create an adapter class.