AS1 OOP: Inheritance
by senocular
Introduction
One of the advantages of OOP is portability and re-use of
code. Inheritance allows new classes to be derived from
pre-existing classes thereby reducing the need to what might
be otherwise repeated code of one class in another. What you
get are subclasses that inherit methods and properties from
pre-existing super classes. These subclasses are then said
to extend the super class as it is everything the super
class is plus more. The terms "subclass" and "super class",
by the way, are just ways of identifying certain classes
involved in inheritance. There is nothing particularly
different about a class when it's a super class other than
the fact its being used by some other class (the subclass).
Often you may hear these referred to as parent class and
child class.
In following the metaphor of inheritance in genetic
terms, think about how you look. The way you look is
dependant on what traits you inherited from your parents.
Your parents got their traits from their parents, and their
parents from their parents, and so on and so forth.
Properties and methods in classes can be inherited the same
way. Super classes represent parents of subclasses which are
the children (hence the alternate terminology of parent and
child class). Subclasses inherit properties and methods
which are present in the super class and can use them as
their own.
Actionscript inheritance, however, is slightly different
from the biological inheritance you get from your parents.
For one, biological inheritance is not compounding. For
example. Your grandmother had 2 arms and your mother had 2
arms but that doesn't mean you'll get 4 arms. You will
biologically always be similar in that you will never be
more than a human is supposed to be. With classes in Flash,
when you have a class inherit from a super class, the
resulting subclass gets everything that super class has and
then adds more through its own definition making a final
product which greater and more feature rich than anything
its based on.
[ subclasses inherit everything and add to that definition ]
Another difference is that Actionscript does not support
multiple inheritance. Multiple inheritance is when a class
is able to inherit from more than one separate classes. When
you inherit traits from your parents, you are inheriting
from both your mother and your father. Actionscript classes
can really only have one or the other. "Okay, son. Which is
it? Mom or dad?" If you absolutely need both, then either
your mother or father would have to first inherit from the
other. So if you have a mother and father to inherit from in
Actionscript, the mother would have to either be a subclass
of the father or the father a subclass of the mother.
[ biological inheritance vs. flash inheritance ]
In this fashion, Actionscript inheritance is linear - its
inheritance chains have no inward branching or inheritance
from multiple unique sources. The only branching you get is
outward from a certain class to other subclasses. There's
nothing preventing many other classes to all inherit from
the same super class and further subclasses inheriting from
them.
[ inheritance can branch outward, not inward ]