File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes one more doubt abt 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 "one more doubt abt generics" Watch "one more doubt abt generics" New topic
Author

one more doubt abt generics

Nisha Pinjarkar
Greenhorn

Joined: Apr 01, 2007
Posts: 20
On page 596(k&b book)

List <? super Animal > dList = new ArrayList<Dog>();

Now, this is wrong as Dog is too low in class hierarchy. Only <Animal> or <Object> is legal.

With this explanation, why is this wrong?

static void basket(List<? super Apple>list){list.add(new Object());}

WHy can't we add Object ? isn't it the super type of Apple?
Or we just cannot use add()?
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
static void basket(List<? super Apple>list){list.add(new Object());}

you are telling compiler, that you provide List with parameter of type Object or Fruit (if I remember correctly) or Apple.

So, it will only allow to insert type, which won't break any of these conditions.

Suppose, you call basket method with parameter of type List<Apple>, and now you want to insert Object. This would corrupt type safety.

Suppose, you want to insert Apple or any sub-class of Apple. This is OK, because it can be safely upcasted to any type, that can occur as parameter in List<? super Apple>
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
example with Integer, Number and Object
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
List<? super Animal> dlist = new ArrayList<Dog>(); //NO
<? super Animal> says that dlist can only hold ref of the List (object of a
class that implements List) object parameterized with Animal or super class
of Animal that is Object in our case.


Thanks,
[ May 22, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Lets see this eg:


The method's argument List<? super Sub1>, does two things, it dictates,
1) what can be passed into the method as an argument and
2) what can be added to that argument list within that method.

? super Sub1 means we can invoke that method with a list that is of type Sub1 or above. In which case the following three are possible:
List<? super Sub1 > l = new ArrayList<Sub1>();
List<? super Sub1 > l2 = new ArrayList<Top>();
List<? super Sub1 > l3 = new ArrayList<Object>();

Inside the method, you can add any object that is of type Sub1 or any of its subtypes ie. it expects an object that is-a Sub1. So that whatever can be done on Sub1 can be done on its subtypes. Therefore the following are allowed.
s.add(new Sub1());
s.add(new Sub2());
s.add(new Sub3());

where Sub1 , Sub2 and Sub3 are of type Sub1.
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Now let's get back to your original question


List <? super Animal > dList = new ArrayList<Dog>(); //1


static void basket(List<? super Apple>list){list.add(new Object());} //2


Here you are comparing a type declaration with a method declaration.

Line 1 is a type declaration and Line 2 is a method declaration.

In Line1, anything Animal and above are allowed.

In Line2, anything Apple and below are allowed to be added to the list.

Hope it is clear.
[ May 22, 2007: Message edited by: M Krishnan ]
Nisha Pinjarkar
Greenhorn

Joined: Apr 01, 2007
Posts: 20
It is perfectly clear now. Thanks a lot!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one more doubt abt generics
 
Similar Threads
Generics
k&b: generics ? syntax
Generics problem- why not taking Animal, Object and super of Dog
generics
<? super Integer>