ADOBE FLEX 3
Developer Guide
101
You also have some flexibility with default parameter values. An interface definition can include function declara-
tions with default parameter values. A method that implements such a function declaration must have a default
parameter value that is a member of the same data type as the value specified in the interface definition, but the
actual value does not have to match. For example, the following code defines an interface that contains a method
with a default parameter value of 3:
interface IGamma
{
function doSomething(param:int = 3):void;
}
The following class definition implements the Igamma interface but uses a different default parameter value:
class Gamma implements IGamma
{
public function doSomething(param:int = 4):void {}
}
The reason for this flexibility is that the rules for implementing an interface are designed specifically to ensure data
type compatibility, and requiring identical parameter names and default parameter values is not necessary to achieve
that objective.
Inheritance
Inheritance is a form of code reuse that allows programmers to develop new classes that are based on existing classes.
The existing classes are often referred to as base classes or superclasses, while the new classes are usually called
subclasses. A key advantage of inheritance is that it allows you to reuse code from a base class yet leave the existing
code unmodified. Moreover, inheritance requires no changes to the way that other classes interact with the base
class. Rather than modifying an existing class that may have been thoroughly tested or may already be in use, using
inheritance you can treat that class as an integrated module that you can extend with additional properties or
methods. Accordingly, you use the
extends keyword to indicate that a class inherits from another class.
Inheritance also allows you to take advantage of polymorphism in your code. Polymorphism is the ability to use a
single method name for a method that behaves differently when applied to different data types. A simple example is
a base class named Shape with two subclasses named Circle and Square. The Shape class defines a method named
area(), which returns the area of the shape. If polymorphism is implemented, you can call the area() method on
objects of type Circle and Square and have the correct calculations done for you. Inheritance enables polymorphism
by allowing subclasses to inherit and redefine, or override, methods from the base class. In the following example,
the
area() method is redefined by the Circle and Square classes:
class Shape
{
public function area():Number
{
return NaN;
}
}
ADOBE FLEX 3
Developer Guide
102
class Circle extends Shape
{
private var radius:Number = 1;
override public function area():Number
{
return (Math.PI * (radius * radius));
}
}
class Square extends Shape
{
private var side:Number = 1;
override public function area():Number
{
return (side * side);
}
}
var cir:Circle = new Circle();
trace(cir.area()); // output: 3.141592653589793
var sq:Square = new Square();
trace(sq.area()); // output: 1
Because each class defines a data type, the use of inheritance creates a special relationship between a base class and
a class that extends it. A subclass is guaranteed to possess all the properties of its base class, which means that an
instance of a subclass can always be substituted for an instance of the base class. For example, if a method defines a
parameter of type Shape, it is legal to pass an argument of type Circle because Circle extends Shape, as in the
following:
function draw(shapeToDraw:Shape) {}
var myCircle:Circle = new Circle();
draw(myCircle);
Instance properties and inheritance
An instance property, whether defined with the function, var, or const keywords, is inherited by all subclasses as
long as the property is not declared with the
private attribute in the base class. For example, the Event class in the
Flash Player API has a number of subclasses that inherit properties common to all event objects.
For some types of events, the Event class contains all the properties necessary to define the event. These types of
events do not require instance properties beyond those defined in the Event class. Examples of such events are the
complete event, which occurs when data has loaded successfully, and the connect event, which occurs when a
network connection has been established.
The following example is an excerpt from the Event class that shows some of the properties and methods that are
inherited by subclasses. Because the properties are inherited, an instance of any subclass can access these properties.
public class Event
{
public function get type():String;
public function get bubbles():Boolean;
...
public function stopPropagation():void {}
public function stopImmediatePropagation():void {}

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.