Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics: lower bound method signature

 
Avike Jorgenssen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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?
 
James Quinton
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Andrea Z�rcher
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic