Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg: Covariant return

 
Sundararaj Govindasamy
Ranch Hand
Posts: 46
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}
 
Jari Timonen
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember: Tulip IS-A Plant, Flower IS-A Plant, Tulip IS-A Flower
 
Sundararaj Govindasamy
Ranch Hand
Posts: 46
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jari,
I accept your point but Can Flower have any knowledge about Tulip ? Does Flower knew about "Tulip extends Flower" ?
 
Jari Timonen
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic