Game Development Reference
In-Depth Information
the relationships of one object to another and the features they
share. Adding a property to both Chair and Sofa then becomes as
simple as adding that property to Furniture. When you extend a
class, the new class becomes its subclass and the original is now
referred to as the superclass; in the previous example the Furniture
is the superclass and the Chair and Sofa are subclasses. There are
some practical limitations to pure inheritance (namely that a class
can only extend one other class) that we
'
ll discuss shortly.
Polymorphism
Although it sounds like an affliction one might develop in a science
fiction novel, polymorphism is basically the idea that one class can
be substituted in code for another and that certain behaviors or
properties of inherited objects can be changed or overridden.
ActionScript only allows for a basic type of polymorhpism, so that
'
s
'
all we
ll cover here. Take the Chair from the previous example on
inheritance. Now, let
'
s say that we extend Chair to make a High-
Chair for an infant. Certain properties of the chair may not apply
or behave differently in the HighChair versus the normal Chair. We
can override the features that are different in the HighChair but
continue to inherit those that are similar. In practice, this process
is not as complicated as it sounds, and I will point it out when it is
used.
Interfaces
A core principle of object-oriented programming is the separation
between an interface and an implementation. An interface is simply
a list of public methods and properties, including their types. An
implementation would be a class that uses that interface to define
what methods and properties will be publicly available to other
classes. This concept can be initially confusing, so let
s look at an
example. Note in this example (and throughout the rest of this
book) that interface names in ActionScript start with a capital I by
convention.
In the section
'
we used an example of a Chair
and Sofa extending from Furniture. However, if you were to intro-
duce another piece of furniture, a Table for instance, you would
now be presented with a problem. While all three of these objects
are Furniture, they have very different uses. The Table has no need
for methods that involve people sitting down, and the other two
have no need for methods that set dishes on them. Theoretically,
you could create a whole structure of inheritance, breaking down
Furniture into SeatingFurniture, DisplayFurniture, SupportFurni-
ture, etc., but you can see that this is becoming extremely
unwieldy. Also, any changes that are made in large inheritance
Inheritance,
Search Nedrilad ::




Custom Search