Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in K&B SCJP 5: topic Covariant types"

 
Meera Nanda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In Page 169(K&B) problem number 8,solution to the same gives Answer D as one of the solution.I think that is wrong since the question asks "if inserted at line 6 ,will compile".

class Plant
{
String getName(){return "plant";}
Plant getType(){return this;}
}
class Flower extends Plant
{
//line 6}
class Tulip extends Flower{
//line 9
}


The options are:
a)Flower getType(){return this;}
b)String getType(){return this;}
c)Plant getType(){return this;}
d)Tulip getType(){return this;}


If the option D is inserted at line 9 the above code will compile but not line 6.

Is my understanding right?

Thanks in advance
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you misread the question. Option D is "Tulip getType() { return new Tulip(); }", not "Tulip getType() { return this; }". This is perfectly valid. (Note that there's nothing wrong with referring to the Tulip class prior to its actual declaration in the source file.)
 
Meera Nanda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kelvin!!!
You are right i misread the question!!!
Thank you once again!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic