29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

In addition a metaclass c<strong>an</strong>not be accessed directly. It c<strong>an</strong> only be access by sending the message<br />

class to a class. For example:<br />

Collection class.<br />

If you evaluate this <strong><strong>an</strong>d</strong> print the result, you will get the name of the Collection classes’ metaclass,<br />

which is of course Collection class (which confusingly looks exactly like the expression which<br />

accessed the metaclass).<br />

32.3.2 The Metaclass hierarchy<br />

Figure 32.1: The metaclass hierarchy for the Collection classes<br />

As you are aware, the classes in the <strong>Smalltalk</strong> system form a hierarchy rooted on the class <strong>Object</strong>.<br />

Thus the class Dictionary inherits from Set which in turn inherits from the class Collection<br />

which ultimately inherits from the class <strong>Object</strong>. This me<strong>an</strong>s that when inst<strong>an</strong>ces of these class are<br />

sent messages, the system c<strong>an</strong> search up through the class hierarchy for methods which implement that<br />

message.<br />

Metaclasses also possesses a metaclass hierarchy. In this hierarchy one metaclass c<strong>an</strong> inherit from<br />

<strong>an</strong>other metaclass. In <strong>Smalltalk</strong> -80 the metaclass hierarchy was constrained to mirror the class<br />

hierarchy. This me<strong>an</strong>s that if some class side behavior is defined in Collection, then it will be<br />

inherited by Set <strong><strong>an</strong>d</strong> Dictionary via their metaclasses. Figure 32.1 presents part of the metaclass<br />

hierarchy for the Collection classes. The net result is that if a class is sent a message, t he system begins<br />

the search for a corresponding method in its metaclass. If it does not find it there, it then looks in the<br />

superclass of its metaclass. This process continues until either a suitable method is found or until the<br />

doesNotUnderst<strong><strong>an</strong>d</strong>: message is triggered.<br />

Figure 32.2 illustrates the relationships between the two hierarchies. Note that dashed arrows<br />

represent inst<strong>an</strong>ce relationships, whereas solid arrows represent inherit<strong>an</strong>ce. Please note the case used<br />

for each label. As c<strong>an</strong> be seen from this figure, this me<strong>an</strong>s that each class is <strong>an</strong> inst<strong>an</strong>ce of its metaclass<br />

<strong><strong>an</strong>d</strong> that the metaclass hierarchy exactly mirrors the class hierarchy. In the figure, the inst<strong>an</strong>ce of the<br />

class Dictionary, labeled as aDictionary, is pres ented in a rounded box, because it is <strong>an</strong> object which<br />

c<strong>an</strong>not create new inst<strong>an</strong>ces. Therefore it is a different kind of inst<strong>an</strong>ce. To make this distinction clear a<br />

different box style is used.<br />

270

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!