File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics: lower bound method signature Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generics: lower bound method signature" Watch "Generics: lower bound method signature" New topic

Generics: lower bound method signature

Avike Jorgenssen

Joined: Oct 13, 2006
Posts: 3
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

Joined: Oct 02, 2006
Posts: 94
<T super MyObject> doesn't make any sense. only wildcard can combine with super.
just remember the simple rule:
<? extends MyObject>
<? super MyObject>
are allowed.

<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

Joined: Oct 13, 2006
Posts: 3
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

Joined: Oct 06, 2006
Posts: 10
Type parameters can't have lower bounds.

Look at this: ][/url]

SCJP 5.0
I agree. Here's the link:
subject: Generics: lower bound method signature
It's not a secret anymore!