• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super in generics

 
Simone Aiello
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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>...
 
Henry Wong
author
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic