Quantcast

« Access Modifiers in ActionScript 3.0 Namespace in ActionScript 3.0 »

Polymorphism in ActionScript 3.0

The term Polymorphism describes multiple possible states for a single property. And for the computers geeks, programmers, coders in us describes it as “a mechanism allowing a given function to have many different specifications, depending on the type(s) to which it is applied”.

More aptly in the world of OOP, Polymorphism is extensively used in an Inheritance relationship. It may not be wrong to point out that Polymorphism in ActionScript 2.0 was not as clear as it should have been. With ActionScript 3.0, it is being clearly demarcated with the keyword Override.

Let us look at some simple sample codes

// Mammal Speech in AS3.0
package
{
	import flash.display.Sprite;
	import flash.util.trace;
	//
	public class MammalSpeech extends Sprite
	{
		public function MammalSpeech()
		{
			var mammals:Array = [new HumanBeing(), new Cat(), new Dog()];
			for each (var mammal:Object in mammals)
			{
				command(mammal);
			}
		}
		private function command(mammal:Mammal)
		{
			mammal.speak();
		}
	}    
	// Base class Mammal
	private class Mammal
	{
		public function speak()
		{
		}
	}     
	// following are derived classes
	private class Cat extends Mammal
	{
		//Override keyword used in derived compulsory to override the Base class
		public override function speak()
		{
			trace("meow!");
		}
	}
	private class Dog extends Mammal
	{
		public override function speak()
		{
			trace("woof!");
		}
	}
	private class HumanBeing extends Mammal
	{
		public override function speak()
		{
			trace("Hi I am santoshkumar spend a lot of time in programming");             }
		}
	}
}

For the above code class Mammal is a Base class(Super) and type of Mammals like HumanBeing, Dog, Cat are derived class(Sub). It is very clear from the fact that HumanBeing, Dogs, Cats etc speak in different ways. So all derived classes must override the Base class method speak. This was an attempt to make an easy implementation of Polymorphism in ActionScript 3.0.

Comments

  1. Tony MacDonell | January 1st, 2006 | 8:11 pm

    Actually, Polymorphism goes far beyond Inhertiance and overriding methods these days.

    The truest expression of it in Actionscript 3 (and 2 as well) is the usage of interfaces, and usage of interfaces as a data type.

    Actionscript 3 actually allows you to type a variable to an Interface. You see this frequently used in the new Flex Framework. By typing a var to an interface, you can easily escape from inheritance pitfalls while maintaining the benefits of polymorphism.

  2. Peter | January 1st, 2006 | 9:12 pm

    when using polymorphism through inheritance you came across that problem in AS2.0 of not having an ability to protect your methods from being overridden.

    I always preferred to use interfaces to enforce polymorphism whenever possible.

  3. Philip | January 10th, 2006 | 8:48 am

    It looks like c# besides no
    virtual keyword is included

    i think there aren’t more changes for new language version …

  4. El | January 24th, 2007 | 5:42 am

    Actually, a question:

    Class A - a super class with method abc();
    Class B - a subclass of A that has overridden abc() method;

    var m:A = new B();

    m.abc() => calls abc() from class B;

    How do I call abc() from class A (syntax)?
    (i’ve tried e.g., m.super.abc() which didn’t work)

    Regards,

    El

  5. RaymonWazerri | April 21st, 2007 | 5:12 am

    Hey,
    I love what you’e doing!
    Don’t ever change and best of luck.

    Raymon W.

Post a comment