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> 1<br />

name: 'John'<br />

age: 31<br />

Class: person<br />

Inst<strong>an</strong>ce variables:<br />

name, age<br />

Methods: birthday<br />

name: 'Peter'<br />

age: 65<br />

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

name: 'Dave'<br />

age: 27<br />

name: 'Chris'<br />

age: 39<br />

<strong>Object</strong> 2 <strong>Object</strong> 3<br />

Figure 6.1: Multiple inst<strong>an</strong>ce variables but a single method<br />

In Figure 6.1 there are four inst<strong>an</strong>ce of the class Person. Each inst<strong>an</strong>ce contains copies of the<br />

inst<strong>an</strong>ce variable definitions for name <strong><strong>an</strong>d</strong> age. Thus enabling them to have their own values for these<br />

inst<strong>an</strong>ce variables. In contrast, each inst<strong>an</strong>ce references the single definition for the method birthday<br />

which is held by the class.<br />

6.3.4 Classes <strong><strong>an</strong>d</strong> inherit<strong>an</strong>ce<br />

It is through classes that <strong>an</strong> object c<strong>an</strong> inherit facilities from other types of objects. T hat is, a subclass<br />

inherits properties from its superclass. For example, in the Person definition above, we stated that<br />

Person was a subclass of <strong>Object</strong>. Therefore, Person inherits all the methods <strong><strong>an</strong>d</strong> inst<strong>an</strong>ce<br />

variables etc. which were defined in <strong>Object</strong> (except those that were overwritten in Person). Thus,<br />

subclasses are used to refine the behavior <strong><strong>an</strong>d</strong> data structures of a superclass. It should be noted that<br />

<strong>Smalltalk</strong> supports single inherit<strong>an</strong>ce while some of the object oriented l<strong>an</strong>guages (most notably C++)<br />

support multiple inherit<strong>an</strong>ce. Multiple inherit<strong>an</strong>ce is where a subclass c<strong>an</strong> inherit from more th<strong>an</strong> one<br />

superclass. However, difficulties c<strong>an</strong> arise when attempting to determine where different methods will<br />

be executed.<br />

6.3.4.1 An example of inherit<strong>an</strong>ce<br />

To illustrate how single inherit<strong>an</strong>ce works consider Figure 6.2. We will assume that we have three<br />

classes called Class1, Class2 <strong><strong>an</strong>d</strong> Class3. Class1 is a subclass of <strong>Object</strong>, Class2 is a<br />

subclass of Class1 <strong><strong>an</strong>d</strong> Class3 is a subclass of Class2.<br />

When <strong>an</strong> inst<strong>an</strong>ce of Class3 is created, it contains all the inst<strong>an</strong>ce variables defined in classes 1 to<br />

3 <strong><strong>an</strong>d</strong> class <strong>Object</strong>. If <strong>an</strong>y inst<strong>an</strong>ce variable possesses the same name as <strong>an</strong> inst<strong>an</strong>ce variable in a<br />

higher class, then only one inst<strong>an</strong>ce variable of that name will be created. We do not need to consider<br />

which one is created as they are both inst<strong>an</strong>ce variables which c<strong>an</strong> take <strong>an</strong>y value (<strong>Smalltalk</strong> is not<br />

strongly typed remember!).<br />

When we have <strong>an</strong> inst<strong>an</strong>ce of Class3 we c<strong>an</strong> send it a message requesting that a particular method<br />

is executed. Remember that methods are held by classes <strong><strong>an</strong>d</strong> not by inst<strong>an</strong>ces. This me<strong>an</strong>s that the<br />

system will first find the class of the inst<strong>an</strong>ce (in this case Class3) <strong><strong>an</strong>d</strong> search that class for the<br />

required method. If the method is fo und, then it is executed <strong><strong>an</strong>d</strong> the search stops. However, if the<br />

method is not found, then the system will search Class3’s immediate super class; in this case<br />

Class2. This process is repeated until the method is found. Eventually, the search thr ough the<br />

superclasses may reach the class <strong>Object</strong> (which is the root class in the <strong>Smalltalk</strong> system). If the<br />

required method is not found here, then the search process terminates <strong><strong>an</strong>d</strong> the<br />

doesNotUnderst<strong><strong>an</strong>d</strong>: method in the class <strong>Object</strong> is executed instead. This method causes <strong>an</strong><br />

exception to be raised stating that the message sent to the original inst<strong>an</strong>ce is not understood.<br />

64

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

Saved successfully!

Ooh no, something went wrong!