What is the reason for this strange behavior? List<? super Number>
"narrower" ("more specialized") than List<?>
, so the code in example 1 below will not work! When I have a List<?>
return type in an overridden method in a base class (interface), it will always have List<? super Number>
the return type of an override method in the Derived class (interface) - it compiles fine in examples like 1-3 and I understand why.
1. The Superinterface (I1) method is general, the subinterface (I2) method is not general. The covariant return in the subinterface (I2) compiles fine:
interface I1 {
public <T> List<? super Number> f() throws IOException;
}
interface I2 extends I1 {
public List<?> f() throws EOFException;
}
2. Both interface methods are common - compile an error in I2 . The Superinterface (I1) method is general, the subsequence method (I2) is also general. Returning covariance in subinterface (I2) gives a compilation error. "Return type is incompatible with I1.f ()":
interface I1 {
public <T> List<? super Number> f() throws IOException;
}
interface I2 extends I1 {
public <T> List<?> f() throws EOFException;
}
3. Both interface methods are not common - they compile an error in I2 . The Superinterface method (I1) is not general, the subsequence method (I2) is also not general. The covariant return in the subinterface (I2) gives the same compilation error as in 2 - "the return type is incompatible with I1.f ()":
interface I1 {
public List<? super Number> f() throws IOException;
}
interface I2 extends I1 {
public List<?> f() throws EOFException;
}
source
share