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.

encapsulation. Attempting to introduce the same level of encapsulation in Ada c<strong>an</strong> be done, but it is not<br />

natural to the l<strong>an</strong>guage.<br />

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

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

Key<br />

Interface<br />

Procedures<br />

Data<br />

Figure 1.3: <strong>Object</strong> structure <strong><strong>an</strong>d</strong> interaction<br />

Figure 1.3 illustrates the way in which encapsulation works within object oriente d l<strong>an</strong>guages. It<br />

shows that <strong>an</strong>ything outside the object c<strong>an</strong> only gain access to the data the object holds via a specific<br />

interface (the black squares). In turn these interfaces trigger procedures which are internal to the object.<br />

These procedures may then a ccess the data directly, use a second procedure as <strong>an</strong> intermediary or call<br />

<strong>an</strong> interface on <strong>an</strong>other object.<br />

1.8 Inherit<strong>an</strong>ce<br />

1.8.1 What is inherit<strong>an</strong>ce?<br />

A class is <strong>an</strong> example of a particular type of thing (for example mammals are a class of <strong>an</strong>imal). In the<br />

object oriented world a class is a definition of the characteristics of that thing. Thus in the case of<br />

mammals, we might define that they have fur, are warm blooded <strong><strong>an</strong>d</strong> produce live young. Animals such<br />

as dogs <strong><strong>an</strong>d</strong> cats are then inst<strong>an</strong>ces of the class mammal. This is all quite obvious <strong><strong>an</strong>d</strong> should not present<br />

a concept problem for <strong>an</strong>yone. However, in most object oriented l<strong>an</strong>guages (the l<strong>an</strong>guage Self being <strong>an</strong><br />

exception) the concept of the class is tightly linked to the concept of inherit<strong>an</strong>ce.<br />

Inherit<strong>an</strong>ce allows us to state that one class is similar to <strong>an</strong>other class but with a specified set of<br />

differences. Another way of putting it, is that we c<strong>an</strong> define all the things which are common about a<br />

class of things, <strong><strong>an</strong>d</strong> then define what is special about each sub grouping within a subclass.<br />

For example, if we have a class defining all the common traits of mammals we c<strong>an</strong> define how<br />

particular categories of mammals differ. Take for example the Duck -billed platypus. This is a quite<br />

extraordinary mammal which differs from other mammals in a number of import<strong>an</strong>t ways. However,<br />

we do not w<strong>an</strong>t to have to define all the things which it has in common with mammals twice. Not only<br />

is this extra work, but we then have two places in which we have to maintain t his information. We c<strong>an</strong><br />

therefore state that a Duck -billed platypus is a class of mammal that differs in that it does not produce<br />

live young (we might also w<strong>an</strong>t to mention its beak etc. but for now we will ignore these issues).<br />

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

An example which is rather closer to home for most computer scientists is illustrated in Figure 1.4. For<br />

this example we will assume that we have been tasked with the job of designing <strong><strong>an</strong>d</strong> implementing <strong>an</strong><br />

administration sy stem for our local University. This system needs to record both employees of, <strong><strong>an</strong>d</strong><br />

students attending, the university. For students we need to record what department they are in, what<br />

subjects/classes they are taking (referred to as modules) or what their t hesis is about. For employees we<br />

need to record the department they work in, what their salary is, what subjects they lecture or research.<br />

If they are a professor we need to record that fact. In the case of professors we might also w<strong>an</strong>t to<br />

record the government bodies they work for.<br />

Figure 1.4 illustrates a class hierarchy diagram for this application. That is, it illustrates each of the<br />

classes we have defined <strong><strong>an</strong>d</strong> from where they inherit their information. There are a number of points<br />

you should note about this diagram:<br />

20

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

Saved successfully!

Ooh no, something went wrong!