• 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

Generics wildcard - ? super type

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

http://java.sun.com/docs/books/tutorial/java/javaOO/genwildcard.html
Here it says "A wildcard with a lower bound is specified as <? super Type> and stands for all types that are supertypes of Type. Note that just as it is not possible to add an object to a collection of unknown type, it is also not legal to add an object to a collection of an unknown type that has a bound." Then how does the below code work correctly ? According to my understanding, it must give 2 errors. One is that you must not be allowed to add anything to wild card types. Also, you must be allowed to add only super types of Integer. Right ?

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an error in the tutorial. When it says "...it is also not legal to add an object to a collection of an unknown type that has a bound." - this should end with: "...that has an upper bound". For a Collection<? extends Foo>, you can get() from the collection (and know that you've gotten a Foo of some sort) but you can't add() to the collection (because it may be a Collection<SomeSpecialSubtypeOfFoo>) . If we replace <? extends Foo> with <? super Foo> then the reverse is true.

[Sreedevi]: Also, you must be allowed to add only super types of Integer.

You can add an Integer or any supertype of Integer. This is how bounded wildcards have been defined. "extends" and "super" don't work exactly they way they do in class definitions; they've been modified slightly.


(disabled smiley)
[ September 09, 2005: Message edited by: Barry Gaunt ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic