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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<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.2: Class <strong><strong>an</strong>d</strong> inst<strong>an</strong>ce relationships<br />

32.4 ClassDescription <strong><strong>an</strong>d</strong> Behavior<br />

One point we ignored above was “what happens when we get to <strong>Object</strong>?”. That is, <strong>Object</strong> is the<br />

root of the class hierarchy in <strong>Smalltalk</strong> <strong><strong>an</strong>d</strong> therefore <strong>Object</strong> does not inherit from <strong>an</strong>y superclass.<br />

This is not a problem for the class hierarchy as <strong>Object</strong> encapsulates all the concepts associated with<br />

being <strong>an</strong> object. However, what happens in the metaclass hierarchy? One possibility is that <strong>Object</strong>’s<br />

metaclass, <strong>Object</strong> class, encapsulates all the concepts associated with being a class. However, in<br />

<strong>Smalltalk</strong>-80 a different <strong><strong>an</strong>d</strong> more sophisticated approach was adopted. (The reasons for adopting this<br />

approach only really become clear later when we discuss the recursive nature of metaclass, for the<br />

moment just accept that the following approach was taken).<br />

In <strong>Smalltalk</strong>-80, <strong>Object</strong> class , the metaclass of <strong>Object</strong> inherits from a class called Class<br />

(See Figure 32.3). This allows <strong>Object</strong> class to define how the class <strong>Object</strong> should behave, while<br />

the concepts associated with being a class are encapsulated within Class. In particular, Class defines<br />

what inst<strong>an</strong>ces of a class should look like <strong><strong>an</strong>d</strong> how they should behave. It also provides facilities which<br />

enable class variable names <strong><strong>an</strong>d</strong> pool (shared) variables.<br />

Behaviour<br />

ClassDescription<br />

Class<br />

<strong>Object</strong> class<br />

Collection<br />

class<br />

Figure 32.3: The metaclass hierarchy up to Behavior<br />

In turn, Class inherits from ClassDescription. This class provides facilities for naming<br />

classes, commenting classes (<strong><strong>an</strong>d</strong> this is why we defined commentTemplateString here) <strong><strong>an</strong>d</strong><br />

naming inst<strong>an</strong>ce variables. ClassDescription was provided as a super class of Class so that<br />

<strong>an</strong>other class called Metaclass (which we will consider in the next section) could also inherit these<br />

facilities.<br />

271

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

Saved successfully!

Ooh no, something went wrong!