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.

6.3.1 Class definitions<br />

In <strong>Smalltalk</strong> the format of a class definition is the following:<br />

NameOfSuperclass subclass: #NameOfClass<br />

inst<strong>an</strong>ceVariableNames: 'instVarName1 instVarName2'<br />

classVariableNames: 'ClassVarName1 ClassVarName2'<br />

poolDictionaries: ''<br />

category: 'Class protocol'<br />

It is not necessary to remember this format precisely as the <strong>Smalltalk</strong> browsers will present the<br />

above as a template for you to fill out whenever you wish to define a new class. The following is <strong>an</strong><br />

example of a class definition:<br />

<strong>Object</strong> subclass: #Person<br />

inst<strong>an</strong>ceVariableNames: 'name age'<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'Example classes'<br />

This definition states that I wish to define a new class, Person, which will be a subclass of the<br />

<strong>Object</strong> class. My new class will possess two inst<strong>an</strong>ce variables called name <strong><strong>an</strong>d</strong> age. It will has no<br />

class variable s or pool dictionaries (we will discuss these later). Finally, it will be part of the class<br />

category ‘Example Classes’. This last field is normally filled in for you by the system. It is derived<br />

from whatever class category you are in when you attempt to define the new class.<br />

Note that our class name is currently a symbol (see below for <strong>an</strong> expl<strong>an</strong>ation of a symbol) denoted<br />

by a #. This is because we have not defined it yet <strong><strong>an</strong>d</strong> it is therefore not a class name. An error would be<br />

raised if we tried just to use it as class name in the definition. As soon as the definition is complete, we<br />

c<strong>an</strong> forget about the #.<br />

However, classes are not just use d as templates, they have three further responsibilities which<br />

include; actually holding the methods, providing facilities for inherit<strong>an</strong>ce <strong><strong>an</strong>d</strong> creating inst<strong>an</strong>ces. We<br />

shall consider each of these separately below.<br />

6.3.2 Classes <strong><strong>an</strong>d</strong> messages<br />

When a message is actu ally sent to <strong>an</strong> object requesting it to perform some service, it is not the object<br />

which possesses the method but the class. This is for efficiency reasons. For example, if each object<br />

possessed a copy of all the methods defined for that class then there w ould be a great deal of<br />

duplication. Instead, only the class possesses the method definitions. Thus when <strong>an</strong> object receives a<br />

message, it searches its class for a method with the name in the message. If its own class does not<br />

possess a method with the appr opriate name, it goes to its class’s superclass <strong><strong>an</strong>d</strong> searches again. This<br />

search process continues up the class hierarchy until either <strong>an</strong> appropriate method is found or the class<br />

hierarchy terminates (with the class <strong>Object</strong>). If this happens <strong>an</strong> error is raised.<br />

If <strong>an</strong> appropriate method is found, then that method is then executed within the context of the object.<br />

This me<strong>an</strong>s that although the definition of the method resides in the class, the method executes within<br />

the object. Thus different objects c<strong>an</strong> be exec uting the same method at the same time but without <strong>an</strong>y<br />

conflict.<br />

Do not confuse methods with inst<strong>an</strong>ce variables. Each inst<strong>an</strong>ce possesses its own copy of the<br />

inst<strong>an</strong>ces variables (as each inst<strong>an</strong>ce possesses its own state). Figure 6.1 illustrates this idea more<br />

clearly.<br />

6.3.3 Inst<strong>an</strong>ces <strong><strong>an</strong>d</strong> inst<strong>an</strong>ce variables<br />

In <strong>Smalltalk</strong> objects are inst<strong>an</strong>ces of classes. All inst<strong>an</strong>ces of a class share the same responses to<br />

messages (methods), but they will contain different data (i.e. they will possess a different “state”). For<br />

example, the inst<strong>an</strong>ces of class Point will all respond in the same way to messages inquiring about the<br />

value of the x coordinate, but may provide different values.<br />

The class defi nition consists of variable declarations <strong><strong>an</strong>d</strong> all method definitions. The different state<br />

of each inst<strong>an</strong>ce is maintained in one or more inst<strong>an</strong>ce variables.<br />

63

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

Saved successfully!

Ooh no, something went wrong!