views:

34

answers:

1

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"

A: 

hi felix-gasca,

I would make a Interface to be able to do what you are asking for. This you you don't have to cast the variable, and it will give you access to your doSomething function. Here is a small example using your your structure: Note that all the classes are in the same root package, hence there is no import statements.

MainApp.as (this is the default application)

package  
{
    import flash.display.Sprite;

    public class MainApp extends Sprite 
    {
        private var parentClass : IParent;
        private var doA : Boolean = false;

        public function MainApp()
        {

            if (doA)
            parentClass = new DerivedClassA();
            else
            parentClass = new DerivedClassB();

            parentClass.doSomething();
        }
    }
}

Parent.as

package  
{
    import flash.display.Sprite;

    public class Parent extends Sprite
    {
        public function Parent()
        {
        }
    }
}

DerivedClassA.as

package  
{
    public class DerivedClassA extends Parent implements IParent
    {
        public function DerivedClassA()
        {
            super();
        }

        public function doSomething() : void 
        {
            trace (this + " done something");
        }
    }
}

DerivedClassB.as

package  
{
    public class DerivedClassB extends Parent implements IParent
    {
        public function DerivedClassB()
        {
            super();
        }

        public function doSomething() : void 
        {
            trace (this + " done something");
        }
    }
}

IParent.as

package  
{
    public interface IParent 
    {
        function doSomething():void;
    }
}

As you can, see using a Interface, you call the function doSomething without the casting. There is no problem about casting variables, but this can give you a more structured code, and it will assure you have the doSomething method in the classes that implement the interface. Hope it helps... G luck

dome