wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt -chapter 2  K& B -self test -q  no 8 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt -chapter 2  K& B -self test -q  no 8" Watch "doubt -chapter 2  K& B -self test -q  no 8" New topic
Author

doubt -chapter 2 K& B -self test -q no 8

Jayati Das
Greenhorn

Joined: Feb 01, 2007
Posts: 18

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. A and D are examples of co-variant returns, i.e., Flower and
Tulip are both subtypes of Plant.
�˚ B is incorrect, String is not a subtype of Plant.
(Objective 1.5



My doubt is since we are adding this line in class Flower ,the return type of flower or subtype (Tulip) would be allowed return type but how is option C correct.Please help me get this concept right .
Thanks in advance.


SCJP5 85%
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

My doubt is since we are adding this line in class Flower ,the return type of flower or subtype (Tulip) would be allowed return type but how is option C correct.Please help me get this concept right .
Thanks in advance.


Option C is a simple example of function overriding.
rule is :
The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass. (More on this in a few pages when we discuss covariant returns.)
Jayati Das
Greenhorn

Joined: Feb 01, 2007
Posts: 18
thks -I guess i had understood it wrongly

Since it has to be the return type or subtype of overridden methods type ,it would be plant and all the three options are applicable .
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

thks -I guess i had understood it wrongly

Most welcome
[ September 12, 2008: Message edited by: bittoo garg ]
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

You can return only those value from a function which are same as the return type or can be implicitly converted to the return type.

In your example, the object can be implicitly converted to its superclass type.

Look at this example

public int test()
{
return 'c';

}

Even the above code will compile and run, since the character 'c' can be implicitly converted into an int by the compiler. The same stands true for your question.

And the overriding which is discussed above is also called "Method overriding using Co-Variant return types"


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Steve S.
Greenhorn

Joined: Dec 15, 2005
Posts: 3
Originally posted by Jayati Das:



My doubt is since we are adding this line in class Flower ,the return type of flower or subtype (Tulip) would be allowed return type but how is option C correct.Please help me get this concept right .
Thanks in advance.


Tulip IS A Plant.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60802
    
  65

"Steve S.", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jayati Das
Greenhorn

Joined: Feb 01, 2007
Posts: 18
Thks ppl i understood the concept .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt -chapter 2 K& B -self test -q no 8
 
Similar Threads
About return type in JAVA
Doubt in K&B SCJP 5: topic Covariant types"
Doubt-Kathy Sierra
K&B doubt
overridding and co-variant returns query