
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
An OOP Chat Example
|
163
be used anywhere its superclass’s type is expected. For example, if we create a vari-
able,
aInstance, whose type is class A, as follows:
var aInstance:A;
we can then legally assign that variable an instance of any subclass of class A:
aInstance = new B( ); // No problem.
A variable typed to a given class can legally store instances of any sub-
classes of that class.
The preceding assignment works because the compiler knows that any instance of
class B has (through inheritance) all the methods and properties defined by class A.
However, the reverse is not true. We cannot assign an instance of A to a variable
whose datatype is B:
var bInstance:B = new A( ); // Error.
That assignment does not work because the compiler cannot guarantee that an
instance of class A will have the methods and properties defined by class B. Hence,
the compiler generates a type mismatch error:
Type mismatch in assignment statement. Found A where B is required.
We’ll return to this topic later in this chapter when we study polymorphism. For a
full discussion of type mismatches, again see Chapter 3.
An OOP Chat Example
Now that we’re familiar with the basic syntax of inheritance, let’s take a high-level
look at how inheritance can be used in a real application. Consider a chat applica-
tion that ...