• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

super in generics

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Simone Aiello
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic