This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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


Win a copy of Head First Android this week in the Android forum!
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: 46

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
}


SCJP 1.6 SCWCD 1.5
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: 46

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reg: Covariant return
 
jQuery in Action, 3rd edition