• 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

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic