1
public class ChildClass extends MovieClip
{
   public function ChildClass()
   {
   }

    public function aChildClassFunction()
     {
     }

}

 public class AnotherChildClass extends MovieClip
{
   public function ChildClass()
   {
   }

    public function aChildClassFunction()
     {
     }

}

Some random function that asks for childClass ( But a lazy programmer just uses MovieClip class )

    public function setChildClassInstance(var mc:MovieClip) 
    {
// How come "mc" represent and be allowed as parameter ??? It's a super-class.. ie. structurally a sub-set of child-class.

          mc.aChildClassFunction(); //<< Would generate run-time error for 2nd function call. 

    }

The above function can be used for any MovieClip instance also

 var childClassInstance:ChildClass ;
 var anotherChildClassInstance:AnotherChildClass ; 

 setChildClassInstance( childClassInstance )   // <<<<<VALID,  .... NO COMPILER ERROR 

 setChildClassInstance( anotherChildClassInstance )  
  //<<< VALID, NO COMPILER ERROR ..  BUT WILL CAUSE RUNTIME ERROR !!!! 
//
//
//

I wonder, how come SUPER CLASS (MovieClip here) be allowed and represent themeselves as parameter for child-classes ( here: ChildClass, AnotherChildClass). Especially because, "SUPERCLASSES" actually are "SUB-SET" of their PARENT-SET , ie. their child-class.

Thanks

Vishwas
  • 1,533
  • 2
  • 19
  • 40
  • To support common capabilities. All dogs bark, but not all dogs have red hair. You don't need a complete instance of the Irish Setter class to get the dog to bark. – Robert Harvey Feb 01 '13 at 15:07
  • But compiler's work is to minimize possible errors. So there are possibilities that someone would use mc.aChildClassFunction(). – Vishwas Feb 01 '13 at 15:57
  • If what is being passed is a superclass/ baseclass, that function shouldn't be available. – Robert Harvey Feb 01 '13 at 16:14
  • I'm pretty sure this _should_ cause a compiler error. All ChildClass' are MovieClips, but not all MovieClips are ChildClass'. – bwroga Feb 01 '13 at 17:10
  • What happens if you assign to the variable instead of just declaring it? (var mc:MovieClip = new MovieClip()) Maybe the compiler isn't catching it because you are passing null to setChildClassInstance instead of an actual MovieClip instance? – bwroga Feb 01 '13 at 17:14
  • The Flex compiler doesn't allow it, you get a compile error. Also do you really have the keyword `var` in that method signature (that generates an error too), or just a typo? For edification, you should clarify which compiler you're using. – Sunil D. Feb 01 '13 at 17:45
  • ah!.. yes.. sorry for confusion, and pointing that out!.. Amm.. i have edited my question. – Vishwas Feb 01 '13 at 18:10

1 Answers1

1

With the following class you can assume the Animal has inherited all the properties of of MovieClip.

package  {
    import flash.display.MovieClip;
    public class Animal extends MovieClip{
        public function Animal(){
            // constructor code
        }
        public function growl():void{
            trace('growl')
        }
    }
}

So lets look at the inheritance tree of MovieClip

MovieClip->Sprite->DisplayObjectContainer->InteractiveObject->DisplayObject->EventDispatcher->Object

And now we can say

Animal->MovieClip->Sprite->DisplayObjectContainer->InteractiveObject->DisplayObject->EventDispatcher->Object<br>

When we type cast an instance of Animal to Sprite.

var sprite:Sprite = new Animal()

we lose the methods that are defined in Animal IE:growl
This is all fine as long as we do not try to access anything defined above in the inheritance tree.
Remember we can type cast down the tree not up.

Using the above class

var sprite:Sprite = new Animal()
sprite.growl() // this will error since sprite does not contain

Now here is an exception to the rule.
MovieClip is dynamic so type casting as MovieClip will not lose the added method(growl).

var mc:MovieClip = new Animal()
mc.growl() // traces growl

Dynamic classes by definition can not have an interfaces associated with them since an interface is a contract to a class that implements them.

The_asMan
  • 6,364
  • 4
  • 23
  • 34