• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic