Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B doubt

 
chenna chaitu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 '>'.
 
chenna chaitu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic