| 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.
|
 |
 |
|
|
subject: Reg: Covariant return
|
|
|