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