Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

kathy siera book self test question6 of generics

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Dieter Quickfend
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic