• 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

S&K Book Chapter 7 question 10

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

okay guys i know i ask too much the last few days
but it really get too hard with generics

so now here is the question

10.
Given:

interface Hungry<E> { void munch(E x); }
interface Carnivore<E extends Animal> extends Hungry<E> {}
interface Herbivore<E extends Plant> extends Hungry<E> {}
abstract class Plant {}
class Grass extends Plant {}
abstract class Animal {}
class Sheep extends Animal implements Herbivore<Sheep> {
public void munch(Sheep x) {}
}
class Wolf extends Animal implements Carnivore<Sheep> {
public void munch(Sheep x) {}
}


Which of the following changes (taken separately) would allow this code to compile? (Choose all that apply.)

a Change the Carnivore interface to

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

b Change the Herbivore interface to

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

c Change the Sheep class to

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

d Change the Sheep class to

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

e Change the Wolf class to

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

f No changes arc necessary.

the answer is


B is correct. The problem with the original code is that Sheep tries to implement Herbivore<Sheep> and Herbivore declares that its type parameter E can be any type that extends Plant. Since a Sheep is not a Plant, Herbivore<Sheep> makes no sense—the type Sheep is outside the allowed range of Herbivore's parameter E. Only solutions that either alter the definition of a Sheep or alter the definition of Herbivore will be able to fix this. So A, E, and F are eliminated. B works, changing the definition of an Herbivore to allow it to eat Sheep solves the problem. C doesn't work because an Herbivore<Plant> must have a munch (Plant) method, not munch(Grass). And D doesn't work, because in D we made Sheep extend Plant, now the Wolf class breaks because its munch (Sheep) method no longer fulfills the contract of Carnivore.(Objective 6.4)


the thing i didnt understant

is why C is invalid
why to have munch (Plant) not munch (Grass)

and about why D is wrong
what it mean by its munch (Sheep) method no longer fulfills the contract of Carnivore

i dont get both of then

am i stupid or looser!

please help me
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

why C is invalid


if we change the code and put option c there then we have



Now at 1 sheep implements Herbivore interface with type Plant.This is correct.But see Herbivore interface is also extending Hungry interface and method munch in Hungry becomes


Now this is the problem,Munch method in class Sheep has Grass as its argument,so method munch of interface Hungry is not correctly implemented here.

why D is wrong



Now if we put option D in the code then we have


Class Wolf is implementing Carnivore<Sheep>,problem here is with generic type sheep.Carnivore interface has generic type <E extends Animal> i.e. E should be sub type of Animal.But in above code it is clear that Sheep is extending Plant not Animal.So this code will give compiler error.

I hope this helps you.

Sunny Mattas
SCJP5
 
Hossam El-Gazzaz
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you sunny
that helped so much
 
There's a city wid manhunt for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic