| Author |
K&B doubt
|
chenna chaitu
Greenhorn
Joined: Jul 27, 2007
Posts: 2
|
|
Hi All, Can you please explain this question <code> 1. class Plant { 2. String getName() { return "plant"; } 3. Plant getType() { return this; } 4. } 5. class Flower extends Plant { 6. // insert code here 7. } 8. class Tulip extends Flower { } Which statement(s), inserted at line 6, will compile? (Choose all that apply.) A. Flower getType() { return this; } B. String getType() { return "this"; } C. Plant getType() { return this; } D. Tulip getType() { return new Tulip(); } Answer: 3 A, C, and D are correct. </code> but the Tulip type can't known by the parent class Flower...
|
 |
paddy Mahadeva Iyer
Ranch Hand
Joined: Feb 10, 2007
Posts: 63
|
|
Hi, B is not correct because it is returning string. D is correct bcause as of java 5 an over riding method can return a type that can be a subclass. qutoting K & B
Must have same return type, except that as of java 5, the return type can be a subclass - this is known as covariant return
In this case we see a multilevel inheritence. Seems this is the answer .if am wrong somebody please correct me.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
sri sri, please check your private messages. You can see them by clicking My Profile. Thanks for trying to use code tags - on JavaRanch they work with '[' and ']' instead of '<' and '>'.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
chenna chaitu
Greenhorn
Joined: Jul 27, 2007
Posts: 2
|
|
|
thank you
|
 |
 |
|
|
subject: K&B doubt
|
|
|