Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Behaviour<br />
Behaviour<br />
class<br />
ClassDescription<br />
ClassDescription<br />
class<br />
Class<br />
Class class<br />
<strong>Object</strong><br />
<strong>Object</strong> class<br />
Collection<br />
Collection<br />
class<br />
Set<br />
Set class<br />
aDictionary<br />
Dictionary<br />
Dictionary<br />
class<br />
Figure 32.4: The metaclasses for Class, ClassDescription <strong><strong>an</strong>d</strong> Behavior<br />
ClassDescription then inherits from a class known as Behavior. The class Behavior<br />
defines how classes should behave (for example how to create new inst<strong>an</strong>ces). In particular it defines<br />
the minimum state necessary for objects that have inst<strong>an</strong>ces.<br />
Together these three classes define what a class is, how it should behave, how new inst <strong>an</strong>ces should<br />
be created <strong><strong>an</strong>d</strong> what a class should look like. Figure 32.3 illustrates the metaclass inherit<strong>an</strong>ce hierarchy<br />
up to the class Behavior.<br />
You may have noticed from the above class names, that Class, ClassDescription <strong><strong>an</strong>d</strong><br />
Behavior are not metaclasses (i.e. they do not have class after their names). They are in fact classes,<br />
thus at the top of the metaclass hierarchy we find (not surprisingly) that metaclasses are classes after all.<br />
This raises two questions, firstly “do Class, ClassDescription <strong><strong>an</strong>d</strong> Behavior have metaclasses?” <strong><strong>an</strong>d</strong> “If<br />
metaclass are really classes, shouldn’t they be inst<strong>an</strong>ces of something?”. In this section we will <strong>an</strong>swer<br />
the first question, the second question is deferred to the next section.<br />
As Class, ClassDescription <strong><strong>an</strong>d</strong> Behavior are classes, they are all inst<strong>an</strong>ces of their<br />
metaclasses. This is illustrated in Figure 32.4. The metaclass of Class is of course Class class<br />
(the terminology c<strong>an</strong> <strong><strong>an</strong>d</strong> will get worse!), the metaclass of ClassDescription is<br />
ClassDescription class <strong><strong>an</strong>d</strong> the metaclass of Behavior is Behavior class.<br />
If you are comfortable with the description so far you have learnt most (if not all) of what you need<br />
to know to exploit the metaclass structure <strong><strong>an</strong>d</strong> t he presence of Class, ClassDescription <strong><strong>an</strong>d</strong><br />
Behavior. If you find that you become lost in the next section don’t worry, much of what is described<br />
there is implementation level detail <strong><strong>an</strong>d</strong> overly convoluted.<br />
32.5 The metaclass concept goes recursive!<br />
32.5.1 What are metaclasses?<br />
A question which was raised in the previous section (but not <strong>an</strong>swered) was “If metaclass are really<br />
classes, shouldn’t they be inst<strong>an</strong>ces of something?”. The <strong>an</strong>swer to this question is “yes”, just like <strong>an</strong>y<br />
class, they are in fact <strong>an</strong> object <strong><strong>an</strong>d</strong> objects are all inst<strong>an</strong>ces of some class. The next question then is, “of<br />
what class is a metaclass <strong>an</strong> inst<strong>an</strong>ce?”. The <strong>an</strong>swer this time, is the class Metaclass (note the<br />
capitalization <strong><strong>an</strong>d</strong> font).<br />
That is, all metaclasses (such as Collection class, <strong>Object</strong> class <strong><strong>an</strong>d</strong> Set class) are<br />
inst<strong>an</strong>ces of the class Metaclass. This is because all metaclasses have the same behavior. They<br />
define the structure, definition <strong><strong>an</strong>d</strong> behavior of a class. Each subsequent class may have a different<br />
272