Preparing for OCPJP 6 exam (that's why I'm using Java 1.6 compiler) I've noticed something unclear to me about Java Generics. Consider the following code:
class A<K extends Number> {
public <V> V useMe1(A<? super V> a) { // OK
return null;
}
public <V> V useMe2(A<? extends V> a) { // OK
return null;
}
public <V> V useMe3(A<V> a) { // ERROR, but why, since 2 above were ok
return null;
}
}
When I try to compile the code (with 1.6 compiler), I get the error:
type parameter V is not within its bound
Despite unusability of the code above, I'm wondering why does compiler think that types <? super V>
and <? extends V>
are matching the class type bound but <V>
is not (since V is matching those both bounds).
I'm not going to modify that code, I want to understand it. The code is taken from sample OCPJP 6 exam question asking "Which line will compile?"