User Guide
134 Object-Oriented Programming in ActionScript
To see how the other access control specifiers affect compilation and execution of the
preceding example, change the
str variable’s access control specifier to private, protected,
or
internal after deleting or commenting out the following line from the AccessControl
class:
trace (myExt.testString); // error if str is not public
Overriding variables not permitted
Properties that are declared with the var or const keywords are inherited, but cannot be
overridden. To override a property means to redefine the property in a subclass. The only type
of property that can be overridden are methods—that is, properties declared with the
function keyword. Although you cannot override an instance variable, you can achieve
similar functionality by creating getter and setter methods for the instance variable and
overriding the methods. For more information, see “Overriding getters and setters”
on page 135.
Overriding methods
To override a method means to redefine the behavior of an inherited method. Static methods
are not inherited and cannot be overridden. Instance methods, however, are inherited by
subclasses and can be overridden as long as the following two criteria are met:
■ The instance method is not declared with the final keyword in the base class. When used
with an instance method, the
final keyword indicates the programmer’s intent to prevent
subclasses from overriding the method.
■ The instance method is not declared with the private access control specifier in the base
class. If a method is marked as
private in the base class, there is no need to use the
override keyword when defining an identically named method in the subclass because
the base class method will not be visible to the subclass.
To override an instance method that meets these criteria, the method definition in the
subclass must use the
override keyword and must match the superclass version of the
method in the following ways:
■ The override method must have the same level of access control as the base class method.
Methods marked as internal have the same level of access control as methods that have no
access control specifier.
■ The override method must have the same number of parameters as the base class method.
■ The override method parameters must have the same data type annotations as the
parameters in the base class method.
■ The override method must have the same return type as the base class method.