GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg: Covariant return Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg: Covariant return" Watch "Reg: Covariant return" New topic
Author

Reg: Covariant return

Sundararaj Govindasamy
Ranch Hand

Joined: Dec 06, 2007
Posts: 45

Hai Folks,
In kathy's book 2nd chapter , self test Question no 8 ...

class Plant{
Plant getType(return this {
}
}
class Flower extends Plant{
//*****insert code here ... to compile without fail
}
}
class Tulip extends Flower{

}
She said in the answers part, the choice , Tulip getType(){return new Tulip()} is Legal.
Here the superclass(Flower) has no knowledge about the subclass (Tulip) right? Then , How it is possible to return a subclass type object from a superclass? Is it right. Folks Help me
}


Former folding tailor
Jari Timonen
Ranch Hand

Joined: Jan 16, 2004
Posts: 232
Remember: Tulip IS-A Plant, Flower IS-A Plant, Tulip IS-A Flower


SCJP 5.0, SCJD URLyBird 1.3.3, OCMJEA 5 (SCEA) Factory Homes
Sundararaj Govindasamy
Ranch Hand

Joined: Dec 06, 2007
Posts: 45

Jari,
I accept your point but Can Flower have any knowledge about Tulip ? Does Flower knew about "Tulip extends Flower" ?
Jari Timonen
Ranch Hand

Joined: Jan 16, 2004
Posts: 232
I was too confused about this issue. It does not have to be same return type as enclosing class. Return type has to be subclass return type.
 
GeeCON Prague 2014
 
subject: Reg: Covariant return