<< Chapter < Page Chapter >> Page >

Sample output from area method of a myrectangle object.

Missing image
Sample output from area method of a MyRectangle object.

Width and height are random values

Once again, however, the values of Width and Height are established from a random number generator each time an object of the MyRectangle class is instantiated, so the actual values for Width , Height , and Area in Figure 5 will change each time the button is clicked.

A click event handler

A click event handler is registered on the button shown in Figure 5. Each time the button is clicked, the event handler uses the output from a randomnumber generator to instantiate an object of one of the following classes and saves it in a variable of type MyShape .

  • MyShape
  • MyCircle
  • MyRectangle

Multiple versions of the method named area

As I explained earlier , an object of the MyShape class will contain one version of the area method, but objects of the other two classes will each contain two versions of the area method.

One version of the method is common among all three objects, and that is the version that returns the text shown in Figure 3.

However, the other version in each of the MyCircle and MyRectangle classes is unique to its class returning values similar to those shown in Figure 4 and Figure 5.

No selection requirement for object of type MyShape

When the area method is called on an object of the MyShape class, there is no requirement to select a "correct" version of the method because that object only contains one version of the method.

Polymorphism kicks in...

Because the area method is defined in the MyShape class and overridden in the MyCircle class and the MyRectangle class, and because the objects of the MyCircle class and the MyRectangle class are saved as type MyShape , polymorphism kicks in when the area method is called on those objects. The overridden method is selected for execution in those cases where the object contains an overriddenversion of the method.

Could easily expand the shape system

Because of polymorphism, I could easily expand this system of shapes by defining new classes for new shapes (such as triangle, octagon, hexagon, etc.) , without any requirement to modify the MyShape class, the MyCircle class, or the MyRectangle class.

Assuming that I cause the classes for the new shapes to extend the MyShape class and properly override the area method to return the correct values, I could then instantiate objects of the new classes, save themas type MyShape , call the area method on those new objects and expect the correct version of the area method to be executed.

The most powerful concept...

This is runtime polymorphism in all its glory, and is probably the most powerful concept in all of OOP.

Discussion and sample code

Will discuss in fragments

I will discuss the code in the five files shown in Figure 1 in fragments. Complete listings of those files are provided in Listing 9 through Listing 13near the end of the lesson.

The file named Polymorph02.mxml

The complete MXML code for the file named Polymorph02.mxml is provided in Listing 1 and also in Listing 9.

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