Next: Type checking classes using
Up: Typing in object-oriented languages:
Previous: Introducing MyType
Our type-checking rules for methods need to accommodate the fact
that methods can be inherited in subclasses, where
the meanings of self and MyType are different from those in the
original class. The difficulty, then, is that within a fixed
object's type, MyType has a fixed meaning (the type of the object), yet
inside a class it will have a more flexible meaning, because methods and
instance variables from the class might also be inherited in a subclass.
In this section we show how to cope with the changing meaning of
self and MyType in classes.
Our goal is to type check methods in a class in such a way that they
are guaranteed to be type-safe no matter how they are inherited.
We saw in the previous section that we avoid typing problems if we replace
the types of methods by subtypes in subclasses. The definition of
matching captures exactly this change in method types between object types.
We say that object
type
matches object type
, written
, iff for each method
of
there is a
corresponding method
of
such that
. Put slightly differently,

It follows that if
is a subclass of
then
. We will see below that matching is weaker than subtyping.
The difference between this and our earlier definition of subtyping for objects
(which looked identical) is the presence of the keyword MyType.
One important point here: In determining whether two object types match
in the above definition, we determine if
without making any assumption on the meaning of MyType.
As a result the two types will certainly be
subtypes in any particular context where MyType stands for some fixed type.
Next: Type checking classes using
Up: Typing in object-oriented languages:
Previous: Introducing MyType
Kim Bruce
10/28/1998