• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why <T extends B> works but <T super B> doesn't?

 
Omni Muss
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying out some examples to understand “extends” and “super” in relation to generics. I was wondering, why does this work;



But this does not:



Why is the second code syntax wrong?
 
Stephan van Hulst
Bartender
Posts: 6587
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the language designers decided that upper bounds on type parameters weren't useful, and therefor should not be allowed. Using the extends keyword, you can put lower bounds on type parameters and wildcards, but you can put upper bounds only on wildcards (e.g. Comparator<? super T>).

Type parameters are mostly useful so you can return specific types. Using the super keyword would return less specific types, and so is not very useful.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!