• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble Understanding <? super T>

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding of <? super T> thus far is that when I use this declaration on a method that accepts a List it should permit the addition only of type <T> or its super type. With this in mind could someone take a shot at explaining why the below doesn't work. I am starting to loose it!! :-/

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

It won't allow Mamal and animal addition. It only means you can pass the super type of Dog but
you can only add Dog and its sub-types, not the super-type. Hope this helps.

The reason is just to support polymorphism. Factually its a Animal list, it could have something like
a Cat objected added. So if it would allow the addition and all, what when you will be taking things
out of your Dog list and Cat object comes. You will be dead according to K&B.

Plus do remember type erasure, at run-time, not type information is available by JVM to support the
legacy code.

Arhaan
 
Greenhorn
Posts: 24
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
basically says that the List passed can be of generic type Dog or any of its superclasses.

This means List<Object>, List<Mamal>, List<Animal> and List<Dog> can be passed to the method. However, what if someone passes a List<Dog> to the addAnimal method? In this case it would be impossible to add an Animal or a Mamal, since they are less specific than Dog.

List<Object>, List<Mamal>, List<Animal> and List<Dog>, however, can all accept a Dog, since a Dog can be implicitly cast to all those types. For this reason the method can only add Dog's or subclasses of Dog to the List.
 
Dave Buchanan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think I should have been thinkin

- accepts any List of Objects that have a super type of type T

e.g. allow addition of Animal, and If .

Thanks everyone, the ranch is really helping me at the moment! :-D
reply
    Bookmark Topic Watch Topic
  • New Topic