• 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

Generics compilation problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I'm experimenting with generics and I encountered this problem. Lines 16 and 17 don't compile. Apparently the compiler prevents me from adding an A or an Object while I specified List<? super B>. Why doesn't this compile? Why can I add a B or a C but not A or Object?
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gert Demol wrote:Apparently the compiler prevents me from adding an A or an Object while I specified List<? super B>.


Yes. A List<? super B> could be a List<B>, List<A>, or List<Object>. The compiler doesn't know which, based on the declared type. So since it could be a List<B>, you can't put an A or an Object in it. But you can always put a B or C in the list, because those will fit into any of the possible types of the list.
 
reply
    Bookmark Topic Watch Topic
  • New Topic