• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics

 
Namit Puri
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why the below decalration is invaild for the commnet 2.
<? super ArrayList> means any instance of AraayList or its supertype.

Map<Object, ? super ArrayList> m = new LinkedHashMap<Object, ArrayList>(); //declaration

m.put(1, new Object()); //2

Thanks!
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The runtime type of the Map expects an ArrayList as the second type parameter, so you can't send an Object.
 
Namit Puri
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you meant to say that at runtime the refernce type doesn't matters, it only looks at the object's type and decided which parameter type is allowed.

Thanks!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What we need is one of Angelika Langer's Generic FAQs

Following this FAQ you can see that we can only call the put method with a second argument that is an ArrayList or a subclass thereof.

The right hand side of the assigment:


does not matter. It's the type of m (Map<Object, ? super ArrayList> ) that matters here.
[ December 07, 2006: Message edited by: Barry Gaunt ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic