Wednesday, August 1, 2012

Object Oriented Concepts: Polymorphism

Object Oriented Concepts: Polymorphism:
Today, I thought to spend some time on object-oriented concepts. Mostly, developers look into this kind of OOPs concepts based articles in two kinds of situations either for application development or interviews preparation !!! :) Anyway, let’s focus on Polymorphism.
As it’s well known that Polymorphism is an object-oriented concept. Polymorphism in AS3 can be approached from two direction or way. One approach is to use Inheritance. The other approach is through Interface.
Abstract Class base Inheritance
Inheritance means to extend a Class. For example,
public class SampleClass extends BaseClass
SampleClass inherits all the public / protected properties and methods of BaseClass. And the BaseClass can be referred as super class of SampleClass. In this kind of situation ActionScript provides the ability to override methods of super class. Only those properties and methods which utilize the public or protected namespaces can be overridden and super class ‘s private properties and methods are not accessible to the class which extending the super class.
Let me take an sample and well known example to explain it:

public class Animal { protected var animalName : String;   public function Animal( animalname:String ) { this.animalName = animalname; } public function getName():String { return animalName; } public function talk() : void {   } public function eat(): void { } }
 

Abstract class Animal, provides basic functionality the animals in app will use. For example, each animal has name. Now we extend the Animal class and create an animal.

public class Dog extends Animal { public function Dog() { super("dog"); } override public function talk() : void { // code } override public function eat() : void { // code } }
 

The Dog Class extends abstract class Animal. So the Dog class is known as concrete Animal class, which override the public methods of base class.

public class PolymorphismDemo extends Sprite { public var animalsArray : Array = [ ]; public function PolymorphismDemo() { var dog : Animal = new Dog(); animalsArray = [ dog ]; for each( var animal:Animal in animalsArray) { trace(animal.getName()); animal.eat(); animal.talk(); } } }
 

You can observe that inheritance being used across the Flash platform. For example, any object on stage is extending DisplayObject, all events are extending base class known as Event.
Polymorphism through Intereface
Now, we are discussing the second approach for polymorphism through Interface. Interface is used to create class that implement a specific contract.

public interface IAnimal { function getName() : String; function eat() : void; function talk() : void; }
 

You can see Ianimal declares methods as its declared in abstract animal class in example of first approach. But if you observer something is missing from above methods is public namespace and brackets containing function body.
Interfaces allow only public function declarations.
You can’t declare properties, static or private methods in interface. Because interface represents a contract that lets outside objects know that public methods can be accessed on a class that implements the interface. Since outside objects can’t access private methods. Public properties can’t added to an interface for good encapsulation. And in case if its required to access properties of Interface then will have an great option getter and setter for properties in interface.

public class Dog implements IAnimal { public function Dog() { } public function getName() : String { return "dog"; } public function talk() : void { // code } public function eat() : void { // code } }
 

Above Dog class implements IAnimal interface. And with interface there is no need to use the override syntax. By using the second approach ( with interface ) classes much more reusable.
Enjoy RIA ..  :)

No comments:

Post a Comment