Overriding Behavior : Override « Class « Flash / Flex / ActionScript






Overriding Behavior

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var normalGull:Seagull = new Seagull();
        var quietGull:Seagull = new QuietSeagull();
        normalGull.squawk(); //The seagull says SQUAAA!'
        quietGull.squawk(); //...
        
        var politeGull:Seagull = new PoliteSeagull(); //A new seagull appears
        politeGull.eat();

    }
  }
}

     class Seagull
    {
        public function get weight():Number
        {
            return 2;
        }

        public function squawk():void
        {
            trace("The seagull says 'SQUAAA!'");
        }
        public function fly():void{
        
        }
        public function eat():void{}
    }

class QuietSeagull extends Seagull
    {
        override public function squawk():void
        {
            trace("...");
        }
    }
     class HungrySeagull extends Seagull
    {
        override public function get weight():Number
        {
            return 1.5;
        }
    }

     class PoliteSeagull extends Seagull
    {
        override public function squawk():void
        {
            super.squawk();
            trace("The shy gull covers his mouth in shame.");
        }

        override public function fly():void
        {
            super.fly();
            trace("The gull lands and apologizes for blocking out the sun.");
        }

        override public function eat():void
        {
            trace("The gull apologizes to the animal it's about to eat.");
            super.eat();
        }
    }
    

        








Related examples in the same category

1.Overriding Instance Methods
2.Invoking an Overridden Instance Method: super.methodName(arg1, arg2, ...argn);
3.override method from parent class