User Guide
Data types 59
Run-time type checking also allows more flexible use of inheritance than does compile-time
checking. By deferring type checking to run time, standard mode allows you to reference
properties of a subclass even if you upcast. An upcast occurs when you use a base class to
declare the type of a class instance, but a subclass to instantiate it. For example, you can create
a class named ClassBase that can be extended (classes with the
final attribute cannot be
extended):
class ClassBase
{
}
You can subsequently create a subclass of ClassBase named ClassExtender, which has one
property named
someString, as follows:
class ClassExtender extends ClassBase
{
var someString:String;
}
Using both classes, you can create a class instance that is declared using the ClassBase data
type, but instantiated using the ClassExtender constructor. Upcasts are considered safe
operations because the base class does not contain any properties or methods that are not in
the subclass.
var myClass:ClassBase = new ClassExtender();
A subclass, however, does contain properties or methods that its base class does not. For
example, the ClassExtender class contains the
someString property, which does not exist in
the ClassBase class. In ActionScript 3.0 standard mode, you can reference this property using
the
myClass instance without generating a compile-time error, as shown in the following
example:
var myClass:ClassBase = new ClassExtender();
myClass.someString = "hello";
// no error in ActionScript 3.0 standard mode
The is operator
The is operator, which is new for ActionScript 3.0, allows you to test whether a variable or
expression is a member of a given data type. In previous versions of ActionScript, the
instanceof operator provided this functionality, but in ActionScript 3.0 the instanceof
operator should not be used to test for data type membership. The
is operator should be used
instead of the
instanceof operator for manual type checking because the expression x
instanceof y
merely checks the prototype chain of x for the existence of y (and in
ActionScript 3.0, the prototype chain does not provide a complete picture of the inheritance
hierarchy).