I have two classes, derivedClassA
and derivedClassB
which both extend parentClass
I'm declaring var o:parentClass
and then, depending on what's going on, I want to cast o
as either being of type derivedClassA
or derivedClassB
.
Essentially, this:
var o:parentClass
...
if(shouldUseA)
o = new derivedClassA();
else
o = new derivedClassB();
o.doSomething();
But it's not working, I'm getting all sorts of errors. Isn't this how class inheritance works? I feel like I'm missing something really fundamental, but I can't figure out what. Am I supposed to be using interfaces instead? Is there another way of doing what I want?
EDIT FOR CLARIFICATION:
- doSomething()
is defined in parentClass
as public, and in the derived classes as override public function doSomething()
- errors I'm getting are "Call to a possibly undefined method doSomething through a reference with static type parentClass." and "Implicit coercion of a value of type derviedClassA to an unrelated type parentClass"