This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes super in generics 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 "super in generics" Watch "super in generics" New topic

super in generics

Simone Aiello

Joined: Oct 13, 2010
Posts: 22
Why is it not possible to use the keyword super in a generic class declaration?

line #2 is ok...(i know the purpose )

why line #1 is wrong?

The important is not what you know, but when you know it...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Wildcards cannot be used in the header of reference type declarations. Supertypes in the extends and implements clauses cannot have wildcards.

However, nested wildcards are not a problem in a reference type declaration header or in an object creation expression:

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Simone Aiello

Joined: Oct 13, 2010
Posts: 22
mmmh...maybe I wasn't clear...
I know how wildcards work... (I hope )

I asked why I can't use the super in a declaration,

whereas I can use the extends.
Ankit Garg

Joined: Aug 03, 2008
Posts: 9400

Think about it, what would be the use of <T super B>. If I create a list as Now what can I do with list?? The compiler can't let you add Short or Long objects to list as T might actually evaluate to Integer at runtime. You can only retrieve elements from list to references of type Object as again T is not known. So <T super Integer> will work the same as <? super Integer>...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Henry Wong

Joined: Sep 28, 2004
Posts: 20029

Simone Aiello wrote:
I asked why I can't use the super in a declaration,

whereas I can use the extends.

Short answer: Because the JLS states that it is not legal.

Longer answer: What purpose would it serve? Since everything inherits from Object, something that must be a type that is super of B, means that it could be type Object. And since everything IS-A Object, it means the container can hold anything. What would the compiler need to type check?


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: super in generics
jQuery in Action, 3rd edition