Implementing complex behaviors/relationships requires flexible structures/types to make things happen... which is where covariant return types come into play.
There is a good primer on Wikipedia about covarient return types... which essentially boils down to this:
// Classes used as return types:
class A {
}
class B extends A {
}
// Classes demonstrating method overriding:
class C {
A getFoo() {
return new A();
}
}
class D extends C {
B getFoo() {
return new B();
}
}
A valid approach? In Java (above 5.0) yes... in ActionScript - no; the notion of covariant return types hasn't been concretely realized. It probably sounds like I left a little 'wiggle room' there huh? Ok... you got me - but before I give you the CodeZORS we really need to understand what all this covariant blah blah is all about.
The Rules
1. 'sub-types' are based on the notion of substitutability
2. preconditions cannot be strengthened in a subclass
3. postconditions cannot be weakened in a subclass
So... essentially the rules state that sub-types grow in specificity (more fully realized) but can still exhibit primitive behavior and that the parents of sub-types are more generalized and are purposable only within their direct context. That last sentence is the key - but I'm not going to spoil the surprise!
The fact is... that covariant return types can be accomplished in ActionScript - not with concrete types but with a dynamic class... like Object.
public class A
{
public function A()
{
// nothing...
}
}
public class B extends A
{
public function B()
{
super();
}
}
public class C
{
public function C()
{
// nothing...
}
public function get foo():Object
{
return new A();
}
}
public class D extends C
{
public function D()
{
super();
}
override public function get foo():Object
{
return new B();
}
}
This gets the job done - and I suppose you could use indeterminate types as well if you want. Anyway... and now for the real treat! Remember the key that I mentioned above... the goodies are in the sample project. Enjoy!
[ sample: covariant return types ]