• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference in "? extends Object" and "Object" in Generics

 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I want to create a HashMap that would hold String keys and values of type [Integer,Date,String.Boolean,Double].
I did

This gave me error while trying to put the different types of Object into the Map as value.
When I reverted back to the code below, everything compiled fine.

I could not relate Generic fundamentals I have read to this difference.
AFAIK instead of talking in terms of Base Object class , that might give ClassCastException at later point of time, its better to use Generic types.

Thanks in Advance

Regards,
Amit
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Generics - Wildcards
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Map<String,? extends Object>, you don't know the actual type - it may be Map<String,Object>, but also Map<String,String> or Map<String,Integer>. Therefore, you cannot put anything into the map. With Map<String,Object>, you know the actual type is Object so you can put any object into the map.
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit punekar wrote:Hello,
I want to create a HashMap that would hold String keys and values of type [Integer,Date,String.Boolean,Double].
I did

This gave me error while trying to put the different types of Object into the Map as value.
When I reverted back to the code below, everything compiled fine.

I could not relate Generic fundamentals I have read to this difference.
AFAIK instead of talking in terms of Base Object class , that might give ClassCastException at later point of time, its better to use Generic types.

Thanks in Advance

Regards,
Amit




by default every API classes are children of Parent- Object class so for any of Wrapper classes you need not no extend that way.
else, better to use


so that you can convert the needed values also.
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit punekar wrote:



Also, AFAIK, you cannot have a wildcard notation in object creation i.e.



will throw compile time error. but not this:



And if you are using wildcard in your references like in your case, you will not be able to *add* to the collection.
This is what I had read in SCJP Sun Certified Programmer for Java5 Study Guide.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
@Rob,
Thanks for the explanation.
It does explain me what I understood wrong about wild-cards. But then I really did not understand in which case I can use "? extends Object" .
@amitabh,
The code compiles fine. But when you try to put Integer,String of different Java Wrapper classes inside it, compiler does not allow as Rob explained.
@Ryan.
I had gone through the Generics tutorial and hence thought of using the "? extends Object". It was my mistake that what I understood from tutorial could not apply to the scenario I was handling.

thanks to all once again for your kind help.

Regards,
Amit

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic