<< Chapter < Page Chapter >> Page >

Polymorphism kicks in

The compiler can't possibly know which type of object will be instantiated as a result of the random process following each button click when the program iscompiled. Therefore, the decision as to which set of methods to call as a result of each button click cannot be determined until runtime. This isthe essence of runtime polymorphism .

The cardinal rule

The type of the object's reference determines which set of method names canbe called on that reference. In this case, the set consists of the four methods declared in and inherited into the interface named IArea , which are shown in the above list .

The type of the object determines which method from the set of allowable names is actually executed.

Run the program

You can run the program to see the outputs produced by repeatedly clicking each of the three buttons inFigure 2.

Discussion and sample code

Will discuss in fragments

I will break the longer files in this application down and discuss them in fragments. Complete listings of all of the files areprovided in Listing 10 through Listing 16 near the end of the lesson.

The file named Interface01.mxml

In keeping with my plan to emphasize ActionScript over Flex in this series of lessons, the MXML file for this application is very simple, instantiating only asingle object of type Driver . A listing of the MXML file is provided in Listing 10 near the end of the lesson.

The file named Driver.as

The class named Driver begins in Listing 1. A complete listing of the file is provided in Listing 11 near the end of the lesson.

Beginning of the class named driver.

package CustomClasses{ import flash.events.*;import mx.containers.HBox;import mx.containers.VBox; import mx.controls.Button;import mx.controls.Label; import mx.controls.TextArea;public class Driver extends VBox{private var textArea:TextArea = new TextArea(); private var myShape:IArea;private var randomChoice:Number;private var radius:uint; private var rectWidth:uint;private var rectHeight:uint;

Listing 1 declares several new instance variables. The most interesting variable is named myShape because it is declared to be of type IArea , which is the name of an interface.

References to objects of the classes MyCircle and MyRectangle will be stored in this variable.

Beginning of the constructor for the Driver class

The constructor for the Driver class begins in Listing 2.

Beginning of the constructor for the driver class.

public function Driver(){//constructor var label:Label = new Label();label.text = "Interface Polymorphism Demo"; label.setStyle("fontSize",14);label.setStyle("color",0xFFFF00); addChild(label);//Put three buttons in an HBoxvar hbox:HBox = new HBox(); addChild(hbox);var areaButton:Button = new Button();areaButton.label = "Area"; hbox.addChild(areaButton);var circButton:Button = new Button();circButton.label = "Circumference"; hbox.addChild(circButton);var volumeButton:Button = new Button();volumeButton.label = "Volume"; hbox.addChild(volumeButton);//Put the text area below the HBox.textArea.width = 245; textArea.height = 80;addChild(textArea);

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask