• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


thks -I guess i had understood it wrongly


Most welcome
[ September 12, 2008: Message edited by: bittoo garg ]
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Steve S.", please check your private messages for an important administrative matter.
 
Jayati Das
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thks ppl i understood the concept .
 
reply
    Bookmark Topic Watch Topic
  • New Topic