• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question regarding Chapter 7 of K&B book

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic