• 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

Generics in Action

 
Greenhorn
Posts: 11
Postgres Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone point out to me what exactly is the difference between AnyClass<T> and AnyClass<? extends T>.

The scenario is I am creating a dynamic factory for one of my application, which shall create the instances of those class objects which is implementing my Parent Interface. The Use Case here is that I am storing all those Classes which has to come from this Factory in a Map<String, Class> (not typesafe) which I am instantiating using reflection.

Assuming my Parent Interface is 'Ainterface' can I have a type safe Map of above as Map<String, Class<Ainterface>> OR Map<String, Class<? extends Ainterface>> OR Both are fine?

Detailed Explanation shall be really helpful!!!

If am not clear, kindly let me know.
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried so far? Did you write class with both approaches?

<A> and <? extends A> and <? super A> are different.

You can try declaring List<Number> and List<? extends Number>
and put some Integer, Double into it.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a class Aclass that implements Ainterface, Aclass.class is not compatible with Class<Ainterface> but it is compatible with Class<? extends Ainterface>. If you use Class<Ainterface> there is only one class object you can add - Ainterface.class.
 
Ankur Kapoor
Greenhorn
Posts: 11
Postgres Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:If you have a class Aclass that implements Ainterface, Aclass.class is not compatible with Class<Ainterface> but it is compatible with Class<? extends Ainterface>. If you use Class<Ainterface> there is only one class object you can add - Ainterface.class.



That is the whole point of Confusion.

Assuming we have Aclass and Bclass which is implementing Ainterface.

Now UC1:


-------------------------------------------------
And UC2:



How come UC1 will work fine and UC2 won't ?? Or Java.lang.Class implementation is bit different from the rest of the Classes ??


 
Raymond Tong
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur Kapoor wrote:UC2:


See the code change above for UC2.
 
Ankur Kapoor
Greenhorn
Posts: 11
Postgres Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for pointing that to me Raymond!!!

the corrected UC2 is :



Now according to Rob, this code wont work, But we all know UC1 will work perfectly fine!!!
so still have my doubts on the same!!!

 
Raymond Tong
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't compare UC1 and UC2 unless UC1 is, which both failed

To make these work, you have to use ? extends Ainterface
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I presume you have been through the standard tutorials (use ctrl-f for "generics" and "wildcards") and Google for Angelika Langer Java Generics FAQ.
reply
    Bookmark Topic Watch Topic
  • New Topic