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.2.1 Some terminology<br />

We will now recap some of the terminology introduced in Part 1 of this book, however we will explain<br />

this terminology with reference to <strong>Smalltalk</strong>.<br />

In <strong>Smalltalk</strong> programs are run or actions are performed by passing messages to <strong><strong>an</strong>d</strong> from objects. A<br />

message is a request for some procedure (referred to in <strong>Smalltalk</strong> terms as a method) to be performed by<br />

the object receiving the message (referred to as the receiver of the message). The object which sent the<br />

message in the first place is referred to as the sender. Just as procedure calls c<strong>an</strong> contain parameters , so<br />

c<strong>an</strong> messages. In addition, just as in some functional l<strong>an</strong>guages, all method executions result in a<br />

response being returned to the sender.<br />

<strong>Smalltalk</strong> is not a strongly typed l<strong>an</strong>guage. That is, you do not specify that a variable will take a<br />

certain type of data structure. Nor does the compiler attempt to check what types a var iable possesses.<br />

However, it is not true to say that <strong>Smalltalk</strong> is not typed. Each object is <strong>an</strong> inst<strong>an</strong>ce of a class. These<br />

classes give <strong>an</strong> object a type (as defined by the class). It is possible to send a message to <strong>an</strong> object to<br />

determine the type of its cl ass. <strong>Smalltalk</strong> is thus a dynamically typed l<strong>an</strong>guage. This is a feature of<br />

<strong>Smalltalk</strong> which promotes its abilities as a rapid application development tool. It also enables the<br />

polymorphic facilities available to be utilized appropriately.<br />

6.2.2 The <strong>Smalltalk</strong> message passing mech<strong>an</strong>ism<br />

The <strong>Smalltalk</strong> message passing mech<strong>an</strong>ism is somewhat like a procedure call in a conventional<br />

l<strong>an</strong>guage. That is:<br />

• The point of control is moved to the receiver; the object sending the message is suspended until a<br />

response is received.<br />

• However, the receiver of a message is not determined when the code is created (at compile time)<br />

it is identified when the message is sent (at run time).<br />

This dynamic (or late) binding mech<strong>an</strong>ism is the feature which gives Sma lltalk its polymorphic<br />

capabilities (see Chapter 1 for a discussion of polymorphism).<br />

Another difference between the message passing mech<strong>an</strong>ism of <strong>Smalltalk</strong> <strong><strong>an</strong>d</strong> the procedure call<br />

mech<strong>an</strong>isms of other l<strong>an</strong>guages, is that much of what one would consider the ba sics of the l<strong>an</strong>guage are<br />

implemented using message passing (for example the equivalent of control <strong><strong>an</strong>d</strong> iterations structures).<br />

This me<strong>an</strong>s that not only is message passing fundamental to the l<strong>an</strong>guage, it is also a critically import<strong>an</strong>t<br />

feature of the perform<strong>an</strong>ce of the l<strong>an</strong>guage.<br />

6.2.3 <strong>Smalltalk</strong> statements<br />

In the remainder of this chapter you will encounter a number of pieces of <strong>Smalltalk</strong>. It is therefore<br />

useful to introduce you to one of the features of the <strong>Smalltalk</strong> l<strong>an</strong>guage; the statement terminator. In<br />

<strong>Smalltalk</strong> this is the full stop (or period). For example:<br />

Tr<strong>an</strong>script show: 'Hello World'.<br />

Thus the majority of statements will terminate with a ‘ .’. There are a few situations in which it is not<br />

necessary to terminate a statement with a full stop. However, in these situations it is often a good idea to<br />

do so. In this <strong><strong>an</strong>d</strong> following chapters, we shall adopt the convention of always terminating a statement<br />

with a full stop (period).<br />

6.3 Classes<br />

A class is the basic building block in <strong>Smalltalk</strong>. Classes act as templates which are used to construct<br />

inst<strong>an</strong>ces. This me<strong>an</strong>s that programmers c<strong>an</strong> specify the structure of <strong>an</strong> object (i.e. what inst<strong>an</strong>ce<br />

variables etc. it will possess) <strong><strong>an</strong>d</strong> the function of <strong>an</strong> object (i.e. what methods it will have) separately<br />

from t he objects themselves. This is import<strong>an</strong>t, as it would be extremely time -consuming (as well as<br />

inefficient) for the programmer to have to define each object individually. Instead, they define a class<br />

<strong><strong>an</strong>d</strong> create inst<strong>an</strong>ces of that class.<br />

62

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

Saved successfully!

Ooh no, something went wrong!