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.

is a controversial subject which is still being debated. Those who think it is useful fail to see why<br />

other l<strong>an</strong>guages don’t include it <strong><strong>an</strong>d</strong> vice versa. Java the most recent object oriented l<strong>an</strong>guage<br />

does not include multiple inherit<strong>an</strong>ce.<br />

1.9 Abstraction<br />

We have already touched on the subject of abstraction in the previous section. However, abstraction is<br />

much more th<strong>an</strong> just the ability to define categories of things which c<strong>an</strong> hold common features of other<br />

categories of th ings (e.g. Student is <strong>an</strong> abstract class of Undergraduate-Student <strong><strong>an</strong>d</strong><br />

Postgraduate-Student). It is in fact a way of specifying what is particular about a class of<br />

things. Often this me<strong>an</strong>s defining the interface for <strong>an</strong> object, the data that such <strong>an</strong> object wi ll hold <strong><strong>an</strong>d</strong><br />

part of the functionality of that object.<br />

For example, we might define a class DataBuffer. This class may be used to define <strong>an</strong> abstract<br />

class for things that hold data <strong><strong>an</strong>d</strong> return them on request. This class may define how the data is to be<br />

held <strong><strong>an</strong>d</strong> that operators such as put() <strong><strong>an</strong>d</strong> get() will be provided to add data to the DataBuffer<br />

<strong><strong>an</strong>d</strong> remove it from the DataBuffer respectively. In turn the implementation of these operators may<br />

be left to those implementing <strong>an</strong>other class which is a subclass of DataBuffer.<br />

The class DataBuffer might, for example, be used to implement a Stack or a Queue. Stack<br />

could implement get() as return the most recent data item added while Queue could implement it as<br />

return the oldest data item held. In either case, a user of either class will know that put() <strong><strong>an</strong>d</strong> get()<br />

are available <strong><strong>an</strong>d</strong> will work in <strong>an</strong> appropriate m<strong>an</strong>ner.<br />

In some l<strong>an</strong>guages it c<strong>an</strong> also be related to protection. For example, both C++ <strong><strong>an</strong>d</strong> Java have the<br />

ability to state whether subclasses are allowed to overwrite data or procedures (<strong><strong>an</strong>d</strong> indeed whether they<br />

have to overwrite them or not). <strong>Smalltalk</strong> does not provide the ability to state that a procedure c<strong>an</strong>not be<br />

overwritten, but it does allow the developer to state that a procedure (or method) is a su bclass<br />

responsibility. That is, a subclass is expected to implement the procedure in order to provide a<br />

functioning class.<br />

Abstraction is also associated with the ability to define Abstract Data Types (or ADTs). In object<br />

oriented terms these are classes (or groups of classes) which provide some sort of behavior (e.g.<br />

DataBuffer above) which acts as the infrastructure for a particular class of data types (e.g. things like<br />

stacks <strong><strong>an</strong>d</strong> queues). However, it is worth pointing ou t that ADTs are more commonly associated with<br />

procedural l<strong>an</strong>guages such as Ada. This is because the concepts in object orientation essentially<br />

supersede ADTs. That is, not only do they encompass all the elements of ADTs, they extend them by<br />

introducing inherit<strong>an</strong>ce.<br />

1.10 Polymorphism<br />

Polymorphism is a str<strong>an</strong>ge sounding (Greek derived) word for a relatively simple concept. It is<br />

essentially the ability to request that the same operation be performed by a wide r<strong>an</strong>ge of different types<br />

of things. How that request is processed will depend on what it is that received the request. However,<br />

you as a programmer, need not worry about how the request is h<strong><strong>an</strong>d</strong>led, only that it is. For example, you<br />

might ask a r<strong>an</strong>ge of objects to provide a printable string describing themselves. This would me<strong>an</strong> that if<br />

you wished to ask <strong>an</strong> inst<strong>an</strong>ce of the Lecturer class (presented above or the system) or a compiler<br />

object or a database object to return such a string, you would apparently use the same interface call<br />

(such as printString in <strong>Smalltalk</strong>).<br />

The name Polymorphism is unfortunate <strong><strong>an</strong>d</strong> often leads to confusion. Of course, it also makes the<br />

whole process sound rather gr<strong><strong>an</strong>d</strong>er th<strong>an</strong> it actually is. There are two types of polymorphism used in<br />

programming l<strong>an</strong>guages: overloadi ng <strong><strong>an</strong>d</strong> overriding. The difference in name relates to how the<br />

mech<strong>an</strong>ism used to resolve what code to execute is determined. The difference between the two is<br />

signific<strong>an</strong>t <strong><strong>an</strong>d</strong> is import<strong>an</strong>t to underst<strong><strong>an</strong>d</strong>. To underst<strong><strong>an</strong>d</strong> what polymorphism actually is, it helps t o<br />

underst<strong><strong>an</strong>d</strong> how these two different mech<strong>an</strong>isms work.<br />

22

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

Saved successfully!

Ooh no, something went wrong!