wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B SCJP 5: topic Covariant types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in K&B SCJP 5: topic Covariant types" " Watch "Doubt in K&B SCJP 5: topic Covariant types" " New topic
Author

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

Meera Nanda
Greenhorn

Joined: Sep 07, 2007
Posts: 5
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


Thank You<br />Meera
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
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.)


SCJP 5.0
Meera Nanda
Greenhorn

Joined: Sep 07, 2007
Posts: 5
Thanks Kelvin!!!
You are right i misread the question!!!
Thank you once again!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in K&B SCJP 5: topic Covariant types"
 
Similar Threads
Doubt-Kathy Sierra
Return type
K&B doubt
doubt -chapter 2 K& B -self test -q no 8
Question from the scjp book