File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B collection and generic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B collection and generic question" Watch "K&B collection and generic question" New topic
Author

K&B collection and generic question

akash azal
Ranch Hand

Joined: Jan 31, 2009
Posts: 70
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 are necessary.

can anyone explain how the output is coming

We will keep things moving!!
Paul Stat
Ranch Hand

Joined: Jan 20, 2009
Posts: 50
Yikes! Use code tags, that's VERY hard to read.
Mumtaz Khan
Ranch Hand

Joined: Feb 21, 2008
Posts: 53

This code will fail to compile at

because the Class Sheep is trying to implement Herbivore interface with type parameter Sheep but type parameter of Herbivore interface is plant or anything that extend plant. Sheep doesn't extend Plant so it will fail to compile.
If we change the interface declaration as
,
where instead of Plant Herbivore parameter is extending Animal now, the code will compile.
So the correct answer should be B.
Lets check other option too.
Answer A,

problem is in Herbivore interface not in Carnivore.
Answer C,

If you declaring Herbivore type parameter as Plant then it should have munch(Plant x) method not munch(Grass x). It will give another compilation error.
Answer D,

Doing this, the wolf class will give compilation error as making class Sheep extending Plant is violating Carnivore interface implementation of Wolf which is taking Sheep as its type parameter, But type parameter should be Animal or any class that extend Animal.
Answer E,

Well compilation problem is in Sheep class making this changes is neither affecting Sheep class nor Herbivore interface.

SCJP 5.0 -- 97%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B collection and generic question
 
Similar Threads
Generic Types extending Abstract implementing Interface
page# 616 question# 10
Generics
Help in understanding generics
K&B -From genric & collections-Self Test