This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma 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


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.
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Reg: Covariant return
 
It's not a secret anymore!