• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question regarding Chapter 7 of K&B book

 
Mark Gortzak
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been pouring over the answer (on page 628/629) to question 16 (on page 616/617). The last line reads "And D doesn't work, because in D we made Sheep extend Plant, not the Wolf class breaks because its munch(Sheep) method no longer fulfills the contract of Carnivore".

I can't figure out *why* D is wrong, or what they mean with that line. I asked some collegue's, but they can't figure it out either.
 
Hasnain Khan
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mark,

Guys correct me if im wrong.

Looking at the <Generic Type> in the interface definition,

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

E is restricted to any type that is Either an Animal class itself or Subclasses of Animal.

Now since sheep extends Plant,

class Sheep extends Plant implements Carnivore<Wolf> {
public void munch(Wolf x) {}
}

replacing E with Sheep causes a compiler error because sheep is not an Animal anymore, its a Plant .

Hope that helped.
Kind Regards.
Hasnain Javed Khan.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic