• 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: lower bound method signature

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why isn't the following method signature allowed?

<T super MyObject> void myMethod(List<T> list){}

The compiler complains about "T super MyObject". I can accomplish it if I use:

<T> void myMethod(List<T super MyObject> list){}

I don't understand why the first signature is considered wrong?
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<T super MyObject> doesn't make any sense. only wildcard can combine with super.
just remember the simple rule:
only
<? extends MyObject>
<? super MyObject>
are allowed.

exception:
<T extends MyObject> can be done when you declare T as a type variable in generic method or class


doesn't compile on my JDK
[ October 13, 2006: Message edited by: James Quinton ]
 
Avike Jorgenssen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uh, neither does it on mine. But I don't understand why it wouldn't "make sense"? Why allow:

static <T extends MyObject> void myMethod(T param) {}

but not:

static <T super MyObject> void myMethod(T param) {}


One sets a lower bound on T, the other sets an upper. What is the reasoning behind allowing one but not the other when they can both be used w�th the wildcard? Isn't "T" working as a labelled wildcard here?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Type parameters can't have lower bounds.

Look at this: ]http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ203[/url]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic