I thought I understood the concepts of generics until I did the
ExamLab test. Could someone, anyone explain these please, please:
abstract class A<K extends Number>
{
//insert code independently here
public abstract <K> K useMe(Object k); // OK
public abstract <K> A<? extends Number> useMe(A<? super Number>k); //OK because K also extends whatever is super to Number - I get that
public abstract <K> A<? extends Number> useMe(A<? super K>k);// "Bound mismatch: The type ? super K is not a valid substitute[...]"- K may extend other interfaces than Number so <? extends Number> does not match <? super K>
public abstract <K> A<? super Number> useMe(A<? extends K>k); // OK. Whatever is super Number will be inherited by whatever is extending K
public abstract <K> A<K> useMe(A<K>k);// "Bound mismatch: The type K is not a valid substitute for the bounded parameter <K extends Number> of the type A<K>" I don't get this one!
public abstract <V extends K> A<V> useMe(Monitor<V>k); //OK If V extends K, it extends Number as well
public abstract <V extends Number> A<V> useMe(A<V>k);// OK use V instead of K
Is this right:
The type deklaration (public abstract <K>...) describes what generic type to use in the method. It can be omitted if it is declared in the class (as in the example). If a new one is introduced, like V, it is needed and is matched to the class deklaration.
The retur type (A<...>) ...they also match the class deklaration.... I see that now. Except I still do not understand the the 5:th method, why does that K match <K extends Number>?? Aaah still need some help, please. Doing the test tomorrow