wood burning stoves 2.0*
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: 43

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 88% ;-)
SCWCD 1.5 97% ;-)
Jari Timonen
Ranch Hand

Joined: Jan 16, 2004
Posts: 230
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: 43

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: 230
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.
 
jQuery in Action, 2nd edition
 
subject: Reg: Covariant return
 
Similar Threads
doubt -chapter 2 K& B -self test -q no 8
K&B doubt
confusion in code regarding inheritance according to java5
Return type
overridding and co-variant returns query