Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B -From genric & collections-Self Test

 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Q10 of selftest in K & B which is


which has options


ans is B
I am not getting that why for option C to be correct we needs munch(Plant p) method instead of munch(Grass x)
also if there is any material which explains about type generic,I am really lost....n I suppose its an imp topic for exam
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

For option C to be correct we need munch(Plant p) method instead of munch(Grass x)!

Because when you replace E with Plant in Interface Herbivore, method's signature would become

void munch(Plant p);

But in the implementation of Sheep, method is overloaded as Grass is used as input parameter instead of Plant.

Since there is no implementation given in Sheep for munch(Plant x), code wont compile!

For this code to compile, either Sheep class has to be abstract or we have to add implementation for method munch(Plant x).

Hope you get it!

 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi your explaination was quite good but I am still confued abt all this



I suppose this is the current scenario
can you explain for each option how it is wrong because according to me the code should compile fine
no
Thank you....I am really not getting the concept I think
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic