Object-oriented ActionScript - Inheritance, Packages, Compilation
(Page 5 of 7 )
When developing an object-oriented application, we can use inheritance to allow one class to adopt the method and property definitions of another. Using inheritance, we can structure an application hierarchically so that many classes can reuse the features of a single class. For example, specific Car, Boat, and Plane classes could reuse the features of a generic Vehicle class, thus reducing redundancy in the application. Less redundancy means less code to write and test. Furthermore, it makes code easier to change—for example, updating a movement algorithm in a single class is easier and less error prone than updating it across several classes.
A class that inherits properties and methods from another class is called a subclass. The class from which a subclass inherits properties and methods is called the sub-class’s superclass. Naturally, a subclass can define its own properties and methods in addition to those it inherits from its superclass. A single superclass can have more than one subclass, but a single subclass can have only one superclass (although it can also inherit from its superclass’s superclass, if any). We’ll cover inheritance in detail in Chapter 6.
Packages In a large application, we can create packages to contain groups of classes. A package lets us organize classes into logical groups and prevents naming conflicts between classes. This is particularly useful when components and third-party class libraries are involved. For example, Flash MX 2004’s GUI components, including one named Button, reside in a package named mx.controls. The GUI component class named Button would be confused with Flash’s built-in Button class if it weren’t identified as part of the mx.controls package. Physically, packages are directories that are collections of class files (i.e., collections of .as files).
We’ll learn about preventing naming conflicts by referring to classes within a package, and much more, in Chapter 9.
Compilation When an OOP application is exported as a Flash movie (i.e., a .swf file), each class is compiled; that is, the compiler attempts to convert each class from source code to bytecode—instructions that the Flash Player can understand and execute. If a class contains errors, compilation fails and the Flash compiler displays the errors in the Output panel in the Flash authoring tool. The error messages, such as the datatype mismatch error described earlier, should help you diagnose and solve the problem. Even if the movie compiles successfully, errors may still occur while a program is running; these are called runtime errors. We’ll learn about Player-generated runtime errors and program-generated runtime errors in Chapter 10.
 | If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!
Visit the O'Reilly Network http://www.oreillynet.com for more online content. |
Next: Starting an Objected-Oriented Application >>
More Flash Articles
More By O'Reilly Media