Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type inheritance

 
Steve Marriott
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why the SCJP Study Guide doesn't like this as a way to 'fix' the code below.
I can get this to compile and I believe it's acceptable because Grass is a type of Plant.

class Sheep extends Animal implements Herbivore<Plant> {
public void munch(Grass x){} }

 
Steve Marriott
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Attention Moderator:
Since there has not been a reply to this post I assume that there is nothing wrong with my solution, which means that there is an error in the book. Can you please transfer this thread to the Errata forum? Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48957
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moderator paying attention:

You are a little bit overoptimistic expecting replies within 1½ hours on Sunday night
Please supply more details, including exactly which book it was, page number etc. Also what they suggest as a correct answer, with brief explanation. Then those of us who have that book can verify you have copied it correctly before even thinking of posting an erratum!
 
Steve Marriott
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys make it seem like there's ALWAYS someone here!

The SCJP 6 Study Guide says that the correct answer to #6 on pg 651 is:

interface Herbivore<E extends Animal> extends Hungry<E> {}

which I agree will work. However, I don't see how answer C doesn't work since Grass IS-A Plant.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48957
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Marriott wrote:You guys make it seem like there's ALWAYS someone here!
Only when you don't want us to be there

That sounds like a generics question, and I can't remember generics just at the moment, so start with the Java™ Tutorials; "generics" appears at least twice on that page. I presume there is an explanation given in the book.
 
Steve Marriott
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Ritchie, the explanation provided in the book as to why answer C is incorrect is the following:

"C doesn't work because an Herbivore<Plant> must have a munch(Plant) method, not munch(Grass)."
 
Campbell Ritchie
Sheriff
Pie
Posts: 48957
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is munch(Plant) not munch(Grass), you will find that a Grass is a Plant, but a <Grass> is not a <Plant> in those tutorial links.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The explanation says that your answer doesn't work because
Herbivore<Plant> must have a munch(Plant), not a munch(Grass)


I'm a little shady on generics.... I think it's because it needs to be able to eat other things (types of plants) than grass...... **shrug**

 
Steve Marriott
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the attempts, but your responses still come up short.

<E> can be any subtype of Plant. Consider how Sheep is a valid type for the munch method parameter in the Carnivore interface. What makes the use of Sheep valid is because it is a subtype of Animal, and the Carnivore interface was defined with <E extends Animal>. In the same way, the Herbivore interface was defined with <E extends Plant>, therefore Grass (which extends Plant) should be a valid type for the munch method parameter in the Herbivore method.

If someone can accurately explain why the above is not correct, I'd appreciate it!

Otherwise, moderators please post this in the Errata forum. Thanks!
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for the sake of discussion, if your answer WERE correct, WHY is it that you wouldn't need it to look like the following:

 
Steve Marriott
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the right question! Thanks!
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes, I guess it's just as important to ask questions as it is to answer them.....

I'm glad I could help.... I think....
Janeice
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic