File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics" Watch "Generics" New topic


Namit Puri
Ranch Hand

Joined: Aug 19, 2006
Posts: 51

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


Namit Puri
SCJP 6.0
"Success is a journey, not a destination." Ben Sweetland
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 19, 2006
Posts: 51
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.

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: Generics
It's not a secret anymore!