File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B doubt" Watch "K&B doubt" New topic

K&B doubt

chenna chaitu

Joined: Jul 27, 2007
Posts: 2
Hi All,
Can you please explain this question
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(); }

3 A, C, and D are correct.


but the Tulip type can't known by the parent class Flower...
paddy Mahadeva Iyer
Ranch Hand

Joined: Feb 10, 2007
Posts: 63

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

Joined: Aug 16, 2005
Posts: 15096

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 8 API documentation
chenna chaitu

Joined: Jul 27, 2007
Posts: 2
thank you
I agree. Here's the link:
subject: K&B doubt
It's not a secret anymore!