O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Defining a Class

To create a new class, we use a class definition, as shown in the following generalized code:

class Identifier {
}

A class definition starts with the keyword class, followed by a class name, represented by Identifier in the preceding code. The term identifier simply refers to a name. Identifiers must not contain spaces or dashes, and cannot start with a number. Class names conventionally use a capital letter for the first, and all subsequent words in the name, as in Date or TextField. (TextField is a built-in Flash-runtime class whose instances represent text that can be displayed on screen.)

The curly braces ({}) following Identifier in the preceding class definition are a block statement, just like the block statement in a package definition. A class definition's block statement is known as the class block or sometimes the class body. The class block contains directives that describe the characteristics and behavior of the class and its instances.

Here's the basic class definition for the main class of our simulated zoo game, VirtualZoo. We place the class definition in the package body, in the file VirtualZoo.as:

package zoo {
  class VirtualZoo {
  }
}

Because the preceding VirtualZoo class definition resides in a package named zoo, the complete name of the class (known as the fully qualified class name) is zoo.VirtualZoo. In casual discussion, however, we'll use the shorter, unqualified class name, VirtualZoo.

Now that we have our program's main class defined, let's create one of the other classes in the program—VirtualPet. From the VirtualPet class, we'll create objects representing pets in the zoo.

Like VirtualZoo, we'll place the source code for the VirtualPet class in the zoo package, in its own file named VirtualPet.as saved in the zoo folder. Here's the code from the VirtualPet.as file:

package zoo {
  class VirtualPet {
  }
}

Notice that a package definition can span multiple source files. Even though VirtualZoo and VirtualPet are stored in different .as files, they belong to the same package, zoo. Any class in any file that resides in a package named zoo is considered part of the zoo package. By contrast, a class definition cannot span multiple files; it must be written, in its entirety, within a single file.

Access Control Modifiers for Classes

By default, a class in a given package can be used by code that also resides in that package only. To make a class available for use outside the package in which it is defined, we must define it with the public attribute. In general terms, a class's attributes dictate how the class and its instances can be used in a program. Attributes are listed before the keyword class in a class definition, as shown in the following generalized code:

attribute class ClassIdentifier {
}

For example, to add the public attribute to the VirtualPet class, we'd use:

package zoo {
  public class VirtualPet {
  }
}

However, in the case of VirtualPet, the public attribute is unnecessary because VirtualPet is used by the VirtualZoo class only, and VirtualZoo can use the VirtualPet class because both classes reside in the zoo package (classes in the same package can always access each other). Hence, we can return to our original VirtualPet definition, which implicitly allows VirtualPet to be used within the zoo package only:

package zoo {
  class VirtualPet {
  }
}

If we wish to explicitly indicate that we intend VirtualPet to be used within the zoo package only, we can use the internal attribute, as shown in the following code:

package zoo {
 internal class VirtualPet {
  }
}

A class defined with the internal attribute can be used within its containing package only. That is, defining a class with the internal attribute is identical to defining the class with no access-control modifier at all. The internal attribute simply serves to make the programmer's intention unambiguous.

The internal and public attributes are known as access-control modifiers because they control the region within which a class can be used (accessed) within a program.

Unlike the VirtualPet class, the VirtualZoo class must be defined with the public attribute because it is the application's main class.

Tip

Adobe's compilers require an application's main class to be defined with the public attribute.

The following code updates VirtualZoo to include the necessary public attribute:

package zoo {
  public class VirtualZoo {
  }
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required