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.

or return something. It has since become more widely used with l<strong>an</strong>guages such as CLOS <strong><strong>an</strong>d</strong><br />

Java also using the term. Two methods are define d in Figure 2.2, one calculates the miles per<br />

gallon while the other one sets the name of the car object.<br />

class Car extends Vehicle<br />

int milage;<br />

int totalFuelUsed;<br />

String name;<br />

define mpg()<br />

begin<br />

return milage / totalFuelUsed;<br />

end<br />

Class<br />

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

variables/<br />

fields<br />

define name(aName)<br />

begin<br />

self name = aName;<br />

end<br />

endClass;<br />

Methods<br />

Reference to<br />

the object within<br />

which the method<br />

executes<br />

Figure 2.2: A pseudo code definition of a class<br />

Message. This is a request from one object to <strong>an</strong>other object requesting some operation or data. It is<br />

traditional to say that one object sends a message to <strong>an</strong>other object requesting it to do<br />

something. The idea is that objects are polite well behaved entities which carry out functions<br />

by sending messages to each other. In other l<strong>an</strong>guages it might be consider akin to a procedure<br />

call (again this issue will be discussed later in part 2).<br />

Self. This is a reference to the object within which the method is executin g (see Figure 2.2). This me<strong>an</strong>s<br />

that it is possible to send messages to this object (i.e. ones’ self).<br />

Single/Multiple inherit<strong>an</strong>ce. Single <strong><strong>an</strong>d</strong> multiple inherit<strong>an</strong>ce refer to the number of superclasses that a<br />

class c<strong>an</strong> inherit from. <strong>Smalltalk</strong> is a single inherit<strong>an</strong>ce system. This me<strong>an</strong>s that a class c<strong>an</strong><br />

only inherit from one superclass. C++ is a multiple inherit<strong>an</strong>ce system. This me<strong>an</strong>s that C++<br />

classes c<strong>an</strong> inherit from one or more classes.<br />

2.3 Types of hierarchies in object orientation<br />

This c<strong>an</strong> be <strong>an</strong> area of confusion. In most object oriented systems there are two types of hierarchy , one<br />

refers to inherit<strong>an</strong>ce (whether single or multiple) while th e other refers to inst<strong>an</strong>tiation. The inherit<strong>an</strong>ce<br />

hierarchy (or is -a hierarchy) has already been described. It is the way in which <strong>an</strong> object “inherits”<br />

features from a superclass.<br />

The inst<strong>an</strong>tiation hierarchy relates to inst<strong>an</strong>ces rather th<strong>an</strong> classes <strong><strong>an</strong>d</strong> is import<strong>an</strong>t during the<br />

execution of the object. There are in fact two types of inst<strong>an</strong>ce hierarchy, one indicates a part -of<br />

relationship while the other relates to a using relationship. Figure 2.3 illustrates the differences between<br />

the two.<br />

Part of relation<br />

or<br />

Reference / Using Relation<br />

Figure 2.3: Inst<strong>an</strong>ce relationships<br />

The difference between <strong>an</strong> is-a relation <strong><strong>an</strong>d</strong> a part-of relationship is often conf using for new<br />

programmers (<strong><strong>an</strong>d</strong> sometimes for those who are experienced in one l<strong>an</strong>guage but are new to <strong>an</strong> object<br />

oriented programming l<strong>an</strong>guage such as <strong>Smalltalk</strong>). Figure 2.4 should make it clear. This figure<br />

illustrates that a student is-a type of person where as <strong>an</strong> engine is part-of a car. That is, it does not<br />

makes sense to say that a student is part-of a person nor that <strong>an</strong> engine is-a type of car!<br />

26

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

Saved successfully!

Ooh no, something went wrong!