CHAPTER 1
16
On line 4 in Figure 1-4, you used the keyword class to signify that the following definition relates to a
class. The class directive can include four additional modifiers, not including its visibility. By default, you
don’t see any of the keywords other than class in your editor, because everything in brackets in Figure 1-
4 is optional; these elements let you make custom modifications to the class.
The first optional modifier is the keyword dynamic. Specifying that your class is dynamic lets you add
properties to your class definition at runtime. Without this modifier, the class is locked: you can’t add new
properties or behaviors later. Dynamic can only be used to modify the definition of the class, not the
properties or methods. Subclasses of a dynamic class can’t inherit dynamic behavior, because it’s specific
to the current definition.
The next modifier specifies the visibility of the class’s definition and defaults to internal, but can be
specified as public to allow any classes outside the declared package view its definition. Although there
are five namespaces that modify visibility, only internal and public can be used to modify the visibility of
a definition.
The last optional attribute, final, specifies that the class can’t be subclassed. Because inheritance is a
significant part of OOP, the use of this final keyword may be confusing, but it enforces data hiding.
Declaring a definition as final prohibits any classes from subclassing the definition. This ensures that the
class can’t be modified, short of physically changing the code in the original file.
Following the class directive, you add a name to identify the definition. To distinguish this from methods
and variables that use camelCase, class names use a capital letter at the start of each word. Class names
should be specific to the behavior they define. An appropriately named class can allude to the behaviors
that a developer expects to find within the definition.
The remaining keywords (extends and implements), again optional, let you add your class to an existing
hierarchy . By default, all classes extend the top-level Object unless specified otherwise. This is why it’s
said that to initialize a class is to instantiate an object. At the core of every class is an Object.
Through the principle of inheritance, your class gains all public and protected, properties and behaviors
of each class in the hierarchy of the chosen superclass.
Suppose we were devising a class named Foo, and Foo requires the abilities possessed by MovieClip.
Choosing to subclass MovieClip looks like the following:
package
{
public class Foo extends MovieClip
{
public function Foo()
{
// constructor code
}
}
}
Because ActionScript doesn’t support inheriting from multiple classes, the keyword implements allows you
to add into your class the interface utilized by a specific type. By implementing an interface, in addition
to adding existing public methods to your definition, you are also adding the interfaces type to the
hierarchical chain of your definition as well.