This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Types extending Abstract implementing Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Types extending Abstract implementing Interface" Watch "Generic Types extending Abstract implementing Interface" New topic
Author

Generic Types extending Abstract implementing Interface

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
Chapter 7, Sierra/Bates, Quiz Question # 6



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

A. Change the Carnivore interface to



B. Change the Herbivore interface to



C. Change the Sheep class to



D. Change the Sheep class to



E. Change the Wolf class to



F. No changes are necessary




I can understand why B is the answer. However, I cannot understand the explanation for D

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.


How is Wolf class broken?

Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
The following code, which incorporates D does not compile:



Compiler outputs:

Eat.java:11: type parameter Sheep is not within its bound
class Wolf extends Animal implements Carnivore<Sheep> {
^
1 error


Please give a hint, thanks!

Faisal A Khan
Greenhorn

Joined: Jul 02, 2010
Posts: 11
Hi Sandra,

By changing Sheep to extend Plant from Animal you have broken the interface contract of Carnivore when used in Wolf. How? By passing it a Sheep which doesn't extend an Animal anymore.

Please let me know if this helps.
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@ Faisal: Now I get it. I changed Sheep to extend Animal and now it compiles.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Types extending Abstract implementing Interface