User Guide

248 Chapter 10: Creating Custom Classes with ActionScript 2.0
This section contains code examples that you can use to become familiar with creating classes in
ActionScript 2.0. If youre not familiar with ActionScript 2.0 scripting, see Chapter 2,
ActionScript Basics,” on page 23 and Chapter 3, “Using Best Practices,” on page 65.
Principles of object-oriented programming
This section provides a brief introduction to principles involved in developing object-oriented
programs. These principles are described in more depth in the rest of this chapter, along with
details on how they are implemented in Macromedia Flash MX 2004 and Macromedia Flash MX
Professional 2004.
To learn about object-oriented programming principles, see the following topics:
“Objects” on page 248
“Classes and class members” on page 248
“Inheritance” on page 249
“Interfaces” on page 249
“Encapsulation on page 249
“Polymorphism” on page 250
Objects
Think of a real-world object, such as a cat. A cat could be said to have properties (or states), such
as name, age, and color; a cat also has behaviors such as sleeping, eating, and purring. In the world
of object-oriented programming, objects also have properties and behaviors. Using object-
oriented techniques, you can model a real-world object (such as a cat) or a more abstract object
(such as a chemical process).
Note: The word “behaviors” is used generically here and does not refer to the Behaviors
development panel in the Flash interface.
Classes and class members
Continuing with the real-world analogy, consider that there are cats of different colors, ages, and
names, with different ways of eating and purring. But despite their individual differences, all cats
are members of the same category, or in object-oriented programming terms, the same class: the
class of cats. In object-oriented programming terminology, each individual cat is said to be an
instance of the cat class.
Likewise, in object-oriented programming, a class defines a blueprint for a type of object. The
characteristics and behaviors that belong to a class are jointly referred to as members of that class.
The characteristics (in the cat example; name, age, and color) are called properties of the class and
are represented as variables; the behaviors (eating, sleeping) are called methods of the class and are
represented as functions.
In ActionScript, you define a class with the
class statement (see “Creating and using classes
on page 254).