• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Compilation Error when using Generics

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Please find the code below.



The below two calls to methods gives a compilation error.
b1.setElement(new GoldenDelicious());
b2.setElement(new Jonagold());

I don't understand why it is doing so, since the type of the baskets b1 and b2 is "anything which extends Apple".
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that both b1 and b2 have the same type. So you can put exactly the same objects into both b1 and b2.

Do you really want that?


since the type of the baskets b1 and b2 is "anything which extends Apple"



No, that's not its type.

It's "a Basket with a type parameter which extends Apple", which is quite different. It could be a Basket<MyVerySpecialAppleWithWormsInIt> as far as the compiler is concerned.
 
That feels good. Thanks. Here's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic