<< Chapter < Page Chapter >> Page >

Listings

  • Listing 1 . Source code for the file named Polymorph02.mxml.
  • Listing 2 . Beginning of the class named Driver.
  • Listing 3 . The constructor for the class named Driver.
  • Listing 4 . Beginning of the method named buttonHandler.
  • Listing 5 . Instantiate an object.
  • Listing 6 . Call the area method on the object.
  • Listing 7 . The class named MyShape.
  • Listing 8 . The class named MyCircle.
  • Listing 9 . Listing of the file named Polymorph02.mxml.
  • Listing 10 . Listing of the file named Driver.as.
  • Listing 11 . Listing of the file named MyShape.as.
  • Listing 12 . Listing of the file named MyCircle.as.
  • Listing 13 . Listing of the file named MyRectangle.as

Supplemental material

I recommend that you also study the other lessons in my extensive collection of online programming tutorials. You will find a consolidated index at www.DickBaldwin.com .

General background information

The first of three major characteristics of an object-oriented program is encapsulation, which I explained in a previous lesson. The second of the threemajor characteristics is inheritance, and the third is polymorphism. I also explained inheritance in a previous lesson. I will explainpolymorphism from a big-picture viewpoint in this lesson.

Not as complicated as it sounds

Upon first reading, you may conclude that polymorphism sounds extremely complicated. However, polymorphism is more difficult to explain than it isto program. Once you have read the description and have seen the concept applied to an actual program, you will (hopefully) conclude that it is not as complicated as it sounds.

What is polymorphism?

Polymorphism is a word taken from the Greek, meaning "many forms", or words to that effect.

The purpose of polymorphism as it applies to OOP is to allow one name to be used to specify a general class of actions. Within that general class ofactions, the specific action that is applied in any particular situation is determined by the type of data involved.

Polymorphism in ActionScript comes into play when inherited methods are overridden to cause them to behave differently for different types of subclassobjects.

Overriding versus overloading methods

If you read much in the area of OOP, you will find the words override and overload used frequently. (This lesson deals with overriding methods and does not deal with overloading.)

Some programming languages such as C++, Java, and C# support a concept known as method or constructor overloading. However, ActionScript 3 does notsupport method or constructor overloading. Overriding a method is an entirely different thing from overloading a method even for those languages thatsupport overloading.

Modify the behavior of an inherited method

Polymorphism can exist when a subclass modifies or customizes the behavior of a method inherited from its superclass in order to meet the special requirementsof objects instantiated from the subclass. This is known as overriding a method and requires the use of the override keyword in ActionScript.

Override methods differently for differentsubclasses

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