wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes kathy siera book self test question6 of generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "kathy siera book self test question6 of generics" Watch "kathy siera book self test question6 of generics" New topic
Author

kathy siera book self test question6 of generics

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


what changes would allow this code to compile?
B change herbivore to
C change sheep class to

D
E change wolf class to


Answer is B given in book.

yes it is right, i agree, B is correct and C is wrong.

but if i try D and E also instead of B, the code also compiled successfully.


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

You should read the book better. The code snippet you gave for D is not consistent with that in the book. In the book, sheep extends Plant, which solves sheep's problem, but doesn't compile because it breaks wolf's contract (carnivore)

and E will only compile if the class already compiles. It doesn't solve any problem with sheep.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Dieter Quickfend wrote:In the book, sheep extends Plant, which solves sheep's problem, but doesn't compile because it breaks wolf's contract (carnivore)

hi thanks, i have edited my post.
i get through with E fine.ok

when i use D, i get
type parameter sheep is not within its bound

how sheep breaks wolf contract please elaborate if you can.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

Wolf is a Carnivore and eats Sheep.
If Sheep IS-A plant, Wolf must be a Herbivore, because Sheep is no longer an Animal (it IS-A Plant).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: kathy siera book self test question6 of generics
 
Similar Threads
Diubt on Generics, Q.10 from K&b book
page# 616 question# 10
K&B -From genric & collections-Self Test
K&B collection and generic question
Generic Types extending Abstract implementing Interface