• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compilation Error when using Generics  RSS feed

 
Ramu Ramaiah
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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".
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!