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.

classes a class inherits both from its immediate parent class, above it in the hierarchy, <strong><strong>an</strong>d</strong> from<br />

classes above the parent. (See the hierarchy in Figure 1.4). This inherit<strong>an</strong>ce mech<strong>an</strong>ism permits<br />

common characteristics of objects to be defined once, but used in m<strong>an</strong>y different places. Any<br />

ch<strong>an</strong>ge is thus localized.<br />

Abstraction. An abstraction denotes the essential characteristics of <strong>an</strong> object that distinguishes it from<br />

all other kinds of objects <strong><strong>an</strong>d</strong> thus provides crisply defined conceptual boundaries, relative to the<br />

perspective of the viewer. That is, it states how a particular object differs from all others.<br />

Polymorphism. This is the ability to send the same message to different inst<strong>an</strong>ces which may appear to<br />

perform the same function. However, the way in which the message is h<strong><strong>an</strong>d</strong>led will depend on<br />

the class of which the inst<strong>an</strong>ce is <strong>an</strong> example.<br />

An interesting question to ask yourself is how do the following l<strong>an</strong>guages relate to the four concepts<br />

related above? ADA, C <strong><strong>an</strong>d</strong> LISP. An obvious issue to consider is r elated to inherit<strong>an</strong>ce. That is, if we<br />

define a concept <strong>an</strong>imal <strong><strong>an</strong>d</strong> we then define a concept dog, we don’t have to redefine all the things<br />

which a dog has in common with other <strong>an</strong>imals. Instead we inherit these features by saying that a dog is<br />

a subclass of <strong>an</strong>imal. This is a feature unique to object oriented l<strong>an</strong>guages. It is also the concept which<br />

promotes (<strong><strong>an</strong>d</strong> achieves) huge amounts of reuse.<br />

1.7 Encapsulation<br />

1.7.1 The concept<br />

Encapsulation or data hiding has been a major feature of a number of programming l<strong>an</strong>guages. For<br />

example, Modula -2 <strong><strong>an</strong>d</strong> Ada both provide extensive encapsulation features. But what exactly is<br />

encapsulation? Essentially, it is the concept of hiding the data behind a software “wall”. Those outside<br />

the wall c<strong>an</strong>not get direct access to that data. Instead they must ask intermediaries (usually the owner of<br />

the data) to provide them with the data.<br />

The adv<strong>an</strong>tage of encapsulation is that the user of the data does not need to know how, where or in<br />

what form the owner of the data stores that data. This me<strong>an</strong>s that if <strong>an</strong>y ch<strong>an</strong>ges are necessary in the<br />

way in which the data is stored, the user of the data need not be affected. That is, they will still ask the<br />

data owner for the data in the same way <strong><strong>an</strong>d</strong> it is only the data owner who must ch<strong>an</strong>ge the way in<br />

which they proceed in fulfilling that request.<br />

Different programming l<strong>an</strong>guages have implemented encapsulation in different ways. For example,<br />

in Ada the prevalent concept which enables encapsulation is the package . A package possess both data<br />

<strong><strong>an</strong>d</strong> procedures. It also specifies a set of interfaces which publish those operations the package wishes to<br />

make available to users of the package. These interfaces may for example implement some operations<br />

or may provide access to data held within the package.<br />

1.7.2 How OO l<strong>an</strong>guages provide encapsulation<br />

<strong>Object</strong> oriented l<strong>an</strong>guages provide encapsulation facilities which present the user of <strong>an</strong> object with a set<br />

of external interfaces. These interfaces say what requests the object will respond to (or in the<br />

terminology of object orientation, which the object will underst<strong><strong>an</strong>d</strong>). These interfaces not only avoid the<br />

need for the caller to underst<strong><strong>an</strong>d</strong> how the internal details of the implementation work, they actually<br />

prevent the user from obtaining that information. That is, the user of <strong>an</strong> object c<strong>an</strong>not directly access the<br />

data held by <strong>an</strong> object as it is not visible to them. In other words, a program that calls this facility c<strong>an</strong><br />

treat the facility as a black box; the program knows what the facil ity’s external interfaces guar<strong>an</strong>tee to<br />

do, <strong><strong>an</strong>d</strong> that is all it needs to know.<br />

It is worth pointing out a difference between the object oriented approach <strong><strong>an</strong>d</strong> the package approach<br />

used in Ada. In general a package will be a large unit of code providing a wide r<strong>an</strong>ge of facilities with a<br />

large number of data structures. For example the Text IO package in Ada. In <strong>an</strong> object oriented<br />

l<strong>an</strong>guage, the encapsulation is provided at the object level. While objects may well be as large <strong><strong>an</strong>d</strong> as<br />

complex as the typical Ada package, they are often much smaller. In l<strong>an</strong>guages such as <strong>Smalltalk</strong> where<br />

everything is <strong>an</strong> object, this me<strong>an</strong>s that the smallest data <strong><strong>an</strong>d</strong> code units also naturally benefit from<br />

19

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

Saved successfully!

Ooh no, something went wrong!