Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt-Kathy Sierra  RSS feed

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

Please consider the following question from Kathy Sierra Book

Given:


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 given is A,C,D

Please Explain the D choice
 
Richa Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I got it.

Question solved.
 
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method can have co-variant return types. Tulip is a sub-class of Plant. So overriding getType method can have a return type which is a sub-class of Plant at any nesting level...

[Oops, it was solved before I could answer ]
 
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:The method can have co-variant return types. Tulip is a sub-class of Plant. So overriding getType method can have a return type which is a sub-class of Plant at any nesting level...

[Oops, it was solved before I could answer ]



Atleast everyone will know why he solved it now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!