• 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

Return type

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class plant
{
plant getType() {return this;}
}

class flower extends Plant{ //insert code here}

class tulip extends flower{}

which will compile inserted at //insert code here ?

A. flower getType(){return this;}

B. String getType(){return this;}

C. Plant getType(){return this;}

A. tulip getType(){return new tulip();}

Answer in K&B is A , C and D are true
I am sure about A and C but not sure about tulip...

as per the polimorphism we can return super type of flower i.e plant or flower itself , but how can we sent value of its subclass ?

Thank you

Nikesh
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Being a case of covariant return type the overriding method in class Flower can return the objects of Plants and its subtype.

A.this returns the reference to a flower object which in turn is a Plant.
B.Compilation error as flower can not be a String.
C.this(flower) is a Plant,so it is fine to place this methods also.
D.Tulip is a Flower and Flower is a Plant which implies Tulip is a Plant.(check with instanceof)
 
Something must be done about this. Let's start by reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic