• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

one more doubt abt generics

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()?
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
example with Integer, Number and Object
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is perfectly clear now. Thanks a lot!
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic