• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Explanation for a question in Generics

 
thanvi janu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,



Question:


Answer was: Fails to compile.

can some one explain me the answer.


Thanks a lot,
thanvi
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you use a wildcard in the declaration of house, the compiler no longer knows it is a AnimalHouse<Cat>. For all it knows it could be an AnimalHouse<Dog>, AnimalHouse<Bird> or even AnimalHouse<String>*. You need to replace the "?" with either "Cat" or "? super Cat".


* Change the declaration of AnimalHouse to this:
This way the generic type is bound to be Animal or any subtype.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic