Datasheet
In Figure 1.2, methods of the superclass Animal are inherited by the subclass Dog. Dog has addi-
tional methods and properties that aren’t shared with its superclass and that can override the
superclass’s existing methods with its own implementations. The same relationship exists between
Dog and Poodle.
Because all versions of
Animal sleep in the same way, calling Dog.sleep() or
Poodle.sleep() actually calls the version of the method implemented in Animal. But because
Dog has its own run() method, calling Dog.run() or Poodle.run() calls that version of the
method. And finally, because all dogs bark in a different way, calling
Poodle.bark() calls a
unique version of the
bark() method that’s implemented in that particular class.
Inheritance allows you to grow an application over time, creating new subclasses as the need for
differing functionality becomes apparent.
In Flex, the ActionScript inheritance model allows you to create extended versions of the compo-
nents included in the Flex class library without modifying the original versions. Then, if an
upgraded version of the original class is delivered by Adobe, a simple recompilation of the applica-
tion that uses the extended class will automatically receive the upgraded features.
Polymorphism
Polymorphism means that you can write methods that accept arguments, or parameters, data typed
as instances of a superclass, but then pass an instance of a subclass to the same method. Because all
subclasses that extend a particular superclass share the same set of methods, properties, and other
object members, the method that expects an instance of the superclass also can accept instances of
the subclass and know that those methods can be called safely.
Polymorphism also can be used with a programming model known as an interface. An interface is
essentially an abstract class that can’t be directly instantiated. Its purpose is to define a set of meth-
ods and other object members and to describe how those methods should be written. But in an
interface such as the one described in Figure 1.4, the method isn’t actually implemented; it only
describes the arguments and return data types that any particular method should have.
A class “implements” an interface by creating concrete versions of the interface’s methods that actu-
ally do something. As with the relationship between super and subclasses, a method might be writ-
ten that accepts an instance of the interface as an argument. At runtime, you actually pass an
instance of the implementing class.
For example, you might decide that
Animal should be abstract; that is, you would never create an
instance of an Animal, only of a particular species. The following code describes the interface:
public interface Animal
{
public function sleep()
{}
}
14
Flex Fundamentals
Part I
06_287644-ch01.qxp 6/23/08 11:28 PM Page 14