Forums Register Login

one more doubt abt generics

+Pie Number of slices to send: Send
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()?
+Pie Number of slices to send: Send
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>
+Pie Number of slices to send: Send
example with Integer, Number and Object
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
It is perfectly clear now. Thanks a lot!
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1201 times.
Similar Threads
k&b: generics ? syntax
generics
Generics problem- why not taking Animal, Object and super of Dog
<? super Integer>
Generics
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:35:03.