This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan 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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "super in generics" Watch "super in generics" New topic
Author

super in generics

Simone Aiello
Greenhorn

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
Greenhorn

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
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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
author
Sheriff

Joined: Sep 28, 2004
Posts: 18104
    
  39

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?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: super in generics
 
Similar Threads
CertPal Question
Generics
Generics question
generics Strike again !!!
<? super Integer>