Consider the following code:
public interface A {
public A another();
}
public interface B {
public B another();
}
public interface AB extends A,B {
public AB another();
}
This leads to a compile error on AB
:
types B and A are incompatible; both define another(), but with unrelated return types
I've seen this SO question, and follow the incompatibility example in the the accepted answer - i.e.
public interface C {
public void doSomething();
}
public interface D {
public boolean doSomething();
}
public interface CD extends C,D {
}
However, in that case the return types were genuinely incompatible - a return type cannot be both void and a boolean. Whereas, in my example above, the another()
return type of AB
is both an A
and a B
, so it is possible to implement both of the extended interfaces.
Furthermore, having looked at the JLS (8.4.8, 8.4.8.3, 8.4.8.4), I don't quite understand why my example above illegal. Can anyone explain this to me?
Second, are there any solutions/workarounds to this other than repeating the contract requirements of A
or B
in AB
?
return AB another();
in youAB
interface? return type should beAB
otherwise it won't compile. – Sudden