aspose file tools*
The moose likes Beginning Java and the fly likes Type inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Type inheritance" Watch "Type inheritance" New topic
Author

Type inheritance

Steve Marriott
Greenhorn

Joined: Jun 06, 2009
Posts: 15
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

Joined: Jun 06, 2009
Posts: 15
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

Joined: Oct 13, 2005
Posts: 38836
    
  23
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

Joined: Jun 06, 2009
Posts: 15
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

Joined: Oct 13, 2005
Posts: 38836
    
  23
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

Joined: Jun 06, 2009
Posts: 15
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

Joined: Oct 13, 2005
Posts: 38836
    
  23
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

Joined: Sep 14, 2009
Posts: 1665
    
  11

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**


When you do things right, people won't be sure you've done anything at all.
Steve Marriott
Greenhorn

Joined: Jun 06, 2009
Posts: 15
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

Joined: Sep 14, 2009
Posts: 1665
    
  11

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

Joined: Jun 06, 2009
Posts: 15
That is the right question! Thanks!
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type inheritance