• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confused over generics

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

...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
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Byju in that case K is the K at the class level, and V is the new type defined for the method...
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Dany Mariana
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic