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.

“There are two kinds of objects in the system, ones that c<strong>an</strong> create inst<strong>an</strong>ces of<br />

themselves (classes) <strong><strong>an</strong>d</strong> ones that c<strong>an</strong> not.”<br />

This is a very import<strong>an</strong>t point, it highlights the fact that everything in <strong>Smalltalk</strong> is <strong>an</strong> object. That is,<br />

everything, including classes, are objects <strong><strong>an</strong>d</strong> c<strong>an</strong> be sent messages (this of course me<strong>an</strong>s that classes<br />

are also inst<strong>an</strong>ces of something, but what this is we will leave until later). However classes are special,<br />

they c<strong>an</strong> create new objec ts called inst<strong>an</strong>ces. Inst<strong>an</strong>ces of classes however c<strong>an</strong>not create other inst<strong>an</strong>ces<br />

(they must ask a class to do it for them).<br />

In fact classes have only a very few roles in the system <strong><strong>an</strong>d</strong> these are essentially limited to:<br />

• creating new inst<strong>an</strong>ces,<br />

• defining what those inst<strong>an</strong>ces will do,<br />

• holding class information (such as class variables).<br />

Whereas inst<strong>an</strong>ces hold application data <strong><strong>an</strong>d</strong> perform the operations defined by the (inst<strong>an</strong>ce)<br />

methods held by the class. That is, the inst<strong>an</strong>ce holds the data, but when it is se nt a message it looks in<br />

the inst<strong>an</strong>ce methods of its class for a method which implements that message. If no method is found in<br />

its class, then its superclass is searched for such a method. This search continues until the class <strong>Object</strong><br />

is reached. If no met hod is found then the message doesNotUnderst<strong><strong>an</strong>d</strong>: is sent to the original<br />

receiver.<br />

32.3 Classes <strong><strong>an</strong>d</strong> metaclasses<br />

32.3.1 Metaclasses<br />

As was indicated in the last section each class is <strong>an</strong> inst<strong>an</strong>ce of a class. However we w<strong>an</strong>t each class to<br />

be able to behave in a different m<strong>an</strong>ner to <strong>an</strong>y other class. Therefore each class must be <strong>an</strong> inst<strong>an</strong>ce of a<br />

different class (as all inst<strong>an</strong>ces of the same class must behave in the same m<strong>an</strong>ner). In <strong>Smalltalk</strong> -80 the<br />

concept of a metaclass was introduced. A metaclass is a special class, whose inst<strong>an</strong>ce is a class. In<br />

general a metaclass only possesses a single inst<strong>an</strong>ce (a class) <strong><strong>an</strong>d</strong> a class will be <strong>an</strong> inst<strong>an</strong>ce of a single<br />

metaclass.<br />

That is, a class is <strong>an</strong> inst<strong>an</strong>ce of a metaclass <strong><strong>an</strong>d</strong> a metaclass defines how a class behaves. This<br />

me<strong>an</strong>s that it is actually in the metaclass that the class methods are defined <strong><strong>an</strong>d</strong> held. This me<strong>an</strong>s that<br />

when a class is sent a message, it looks in its metaclass for the method which implements the message.<br />

In effect, when you click on the class radio button in one of the various browsers in VisualWorks you<br />

are actually examining the classes’ metaclass 16 .<br />

If you are confused by the terminology try to think of it in this way:<br />

“A metaclass defines information about a class (hence the term meta)”.<br />

Metaclasses are not actuall y named directly, instead their names are a combination of the name of<br />

the class they define <strong><strong>an</strong>d</strong> “ class”. Thus the metaclass of Collection is Collection class, the<br />

metaclass of <strong>Object</strong> is <strong>Object</strong> class <strong><strong>an</strong>d</strong> the metaclass of a class called Employee would be<br />

Employee class. It is at this point that people begin to be confused by the terminology. We shall<br />

therefore attempt to recap.<br />

• A class is <strong>an</strong> object which c<strong>an</strong> create inst<strong>an</strong>ces.<br />

• A metaclass is a class which defines a class.<br />

• A metaclass has only one inst<strong>an</strong>ce (the class it defines).<br />

• A metaclass is named after the class it defines concatenated with the word class.<br />

16<br />

In fact if you examine the Browser class you will find references to self meta. This is a reference to whether the user is<br />

currently examining the inst<strong>an</strong>ce side (meta = false) or the class side (meta = true).<br />

269

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

Saved successfully!

Ooh no, something went wrong!