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.

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

Class1<br />

Class 2<br />

method<br />

search<br />

Class 3<br />

message<br />

Figure 6.2: Class inherit<strong>an</strong>ce in <strong>Smalltalk</strong><br />

This search process is repeated every time a message is sent to the inst<strong>an</strong>ce of Class3. Thus, if the<br />

method which matches the original message sends a message to itself (i.e. the inst<strong>an</strong>ce of Class3),<br />

then the search for that method starts again in Class3 (even if the original metho d was found in<br />

Class1).<br />

6.3.4.2 The Yo-Yo problem<br />

The process described above c<strong>an</strong> pose a problem for a programmer trying to follow the execution of the<br />

system by tracing methods <strong><strong>an</strong>d</strong> method execution. This problem is known as the Yo -Yo problem (see<br />

Figure 6.3). It occurs because, every time you encounter a message which is sent to “self” you must<br />

start searching from your own class. This may result in the programmer jumping up <strong><strong>an</strong>d</strong> down the class<br />

hierarchy.<br />

<strong>Object</strong> Class<br />

Class1<br />

Class 2<br />

Class 3<br />

Figure 6.3: The Yo-Yo Problem<br />

The problem occurs because you know that the execution search will start in the current inst<strong>an</strong>ces<br />

class, even if the method which sends the message is defined in a superclass of the current class. In the<br />

figure, the programmer starts the search in Class3, but finds the method definition in Class1,<br />

however this method sends a message to “self” which me<strong>an</strong>s that the programmer must restart the<br />

search in Class3. This time, the method defi nition is found in the class <strong>Object</strong> etc. Even with the<br />

browsing tools provided, this c<strong>an</strong> still be a tedious <strong><strong>an</strong>d</strong> confusing process (particularly for those new to<br />

<strong>Smalltalk</strong>).<br />

6.3.5 Inst<strong>an</strong>ce creation<br />

Classes are also the things which construct the inst<strong>an</strong>ces. They do so in response to a class message. It is<br />

probably confusing, but classes c<strong>an</strong> possess class specific methods as well as class inst<strong>an</strong>ce variable s.<br />

These are often referred to as class side methods <strong><strong>an</strong>d</strong> variables. They c<strong>an</strong> the n respond to a message as<br />

<strong>an</strong> inst<strong>an</strong>ce would (this is because classes are in fact special inst<strong>an</strong>ces - we will discuss this in a later<br />

chapter. For the moment merely accept that classes c<strong>an</strong> be sent messages).<br />

The message most commonly sent to a class is the message new. This message causes a method to<br />

execute which constructs a new inst<strong>an</strong>ce of the class. This process is referred to as inst<strong>an</strong>tiation. You do<br />

not need to know the details of this process. An example of sending the message new to a class<br />

Person is presented below:<br />

Person new.<br />

65

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

Saved successfully!

Ooh no, something went wrong!