File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Generics: lower bound method signature

Avike Jorgenssen
Greenhorn

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:
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

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
Greenhorn

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

Look at this: ]http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeArguments.html#FAQ203[/url]


SCJP 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics: lower bound method signature
 
Similar Threads
Garbage Collection Question
Generics
Question
get Property-Field-Name via Reflection
? extends String