| Author |
Confused over generics
|
Linda Wiklund
Greenhorn
Joined: Oct 22, 2009
Posts: 11
|
|
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
|
 |
Linda Wiklund
Greenhorn
Joined: Oct 22, 2009
Posts: 11
|
|
In my mind this should not work, but it does!!
public abstract <V extends K> A<? extends V> useMe(A<? super K>k); //<? super K> does not match <K extends Number>, K may implement something else.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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!
Linda here in the method declaration, <K> is different from the <K> defined at the class level. So the following two codes are functionally the same
If you remove the <K> part from the method declaration, then the method will compile as the K will then point to the K defined at the class level
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Byju Joy
Ranch Hand
Joined: Sep 06, 2005
Posts: 84
|
|
...here in the method declaration, <K> is different from the <K> defined at the class level.
public abstract <V extends K> void method()
Here type variable declared is V, and K is supposed to be declared at previously at class level. Right?
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
Byju in that case K is the K at the class level, and V is the new type defined for the method...
|
 |
Dany Mariana
Greenhorn
Joined: Oct 31, 2009
Posts: 12
|
|
Hello everyone,
Could you please explain to me why this works:
abstract class A<K extends Number>
{
//insert code independently here
public abstract <K> A<? extends Number> useMe(A<? super Number>k)
Why A<K extends Number> accept A<? super Number> ?
In my mind A<K extends Number> this means: A<Number>, A<Integer>, A<Short> etc but A<? super Number> means A<Object> and A<Number> so this are different.
I was spending all my yesterday evening and today reading about wildcards. I think I am missing something but I don't know what. Please help me
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Dany Cleo wrote:Hello everyone,
Could you please explain to me why this works:
Dany,
Please don't hijack other people's topics. Start a new topic for a new question.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Dany Mariana
Greenhorn
Joined: Oct 31, 2009
Posts: 12
|
|
Please excuse me. I am new and I didn't know that it is better to begin another topic.
Sorry!
Henry Wong wrote:
Dany Cleo wrote:Hello everyone,
Could you please explain to me why this works:
Dany,
Please don't hijack other people's topics. Start a new topic for a new question.
Henry
|
 |
 |
|
|
subject: Confused over generics
|
|
|