Chapter 1
Thinking Ob ject Oriented
Ob ject-oriented programming has b ecome exceedingly p opular in the past few years. Soft-
ware pro ducers rush to release ob ject-oriented versions of their pro ducts. Countless b o oks
and sp ecial issues of academic and trade journals have app eared on the sub ject. Students
strive to b e able somehow to list \exp erience in ob ject-oriented programming" on their re-
sumes. To judge from this frantic activity, ob ject-oriented programming is b eing greeted
with an even greater p opularity than wehave seen in the past heralding earlier revolutionary
ideas, such as \structured programming" or \exp ert systems".
My intent in this rst chapter is to investigate and explain the basic principles of ob ject
oriented programming, and in doing so to illustrate the following two prop ositions:
OOP is a revolutionary idea, totally unlikeanything that has come b efore in program-
ming.
OOP is an evolutionary step, following naturally on the heels of earlier programming
abstractions.
1.1 Why is Ob ject-Oriented Programming Popular?
To judge from much of the p opular press, the following represent a few of the p ossible
reasons why Ob ject-oriented programming has, in the past decade, b ecome so p opular:
The hop e that it will quickly and easily lead to increased pro ductivity and improved
reliability help solve the software crises.
The desire for an easy transition from existing languages.
The resonant similarity to techniques of thinking ab out problems in other domains. 1
2 CHAPTER 1. THINKING OBJECT ORIENTED
Ob ject-Oriented programming is just the latest in a long series of solutions that have b een
prop osed to help solve the \software crises". At heart, the software crises simply means that
our imaginations, and the tasks wewould like to solve with the help of computers, almost
always nearly outstrip our abilities.
But, while ob ject-oriented techniques do facilitate the creation of complex software sys-
tems, it is imp ortant to rememb er that OOP is not a \silver bullet", a term made p opular
byFred Bro oks [Bro oks 87]. Programming a computer is still one of the most dicult
tasks ever undertaken byhumankind; b ecoming pro cient in programming requires talent,
creativity,intelligence, logic, the ability to build and use abstractions, and exp erience { even
when the b est of to ols are available.
I susp ect another reason for the particular p opularity of languages such as C++ and
Ob ject Pascal as opp osed to languages such as Smalltalk and Beta is that managers and
programmers alikehopethataCorPascal programmer can b e changed into a C++ or
Ob ject Pascal programmer with no more e ort than the addition of twocharacters to the
programmer's job title. Unfortunately, this hop e is a long way from b eing realized. Ob ject-
Oriented programming is a new way of thinking ab out what it means to compute, ab out how
we can structure information inside a computer. To b ecome pro cient in ob ject-oriented
techniques requires a complete reevaluation of traditional software development techniques.
1.2 Language and Thought
\Human b eings do not live in the ob jectiveworld alone, nor alone in the world
of so cial activity as ordinarily understo o d, but are very much at the mercy of
the particular language which has b ecome the medium of expression for their
so ciety. It is quite an illusion to imagine that one adjusts to reality essentially
without the use of language and that language is merely an incidental means of
solving sp eci c problems of communication or re ection. The fact of the matter
is that the `real world' is to a large extent unconsciously built up on the language
habits of the group.... We see and hear and otherwise exp erience very largely as
we do b ecause the language habits of our community predisp ose certain choices
of interpretation."
Edward Sapir quoted in [Whorf 56]
This quote emphasizes the fact that the languages we sp eak in uence directly the wayin
whichwe view the world. This is true not only for natural languages, such as the kind studied
by the early 20th century American linguists Edward Sapir and Benjamin Lee Whorf, but
also for arti cial languages such as those we use in programming computers.
1.2.1 Eskimos and Snow
An almost universally cited example of the phenomenon of languages in uencing thought,
although also an erroneous one see [Pullum 91] is the \fact" that Eskimo or Inuit lan-
1.2. LANGUAGE AND THOUGHT 3
guages have manywords to describ e various typ es of snow{wet, u y, heavy, icy, and so
on. This is not surprising. Any community with common interests will naturally develop a
sp ecialized vo cabulary for concepts they wish to discuss.
What is imp ortant is to not over generalize the conclusion we can draw from this simple
observation. It is not that the Eskimo eyeisinany signi cant resp ect di erent from my
own, or that Eskimos can see things that I can not p erceive. With time and training I could
do just as well at di erentiating typ es of snow. But the language I sp eak namely, English
do es not force me into doing so, and so it is not natural to me. A di erent language such
as Inuktitut thus can lead one but do es not require one to view the world in a di erent
fashion.
To make e ective use of ob ject oriented principles requires one to view the world in a new
fashion. But simply using an ob ject oriented language such as C++ do es not, by itself,
force one to b ecome an ob ject oriented programmer. While the use of an ob ject oriented
language will simplify the development of ob ject oriented solutions, it is true, as it has b een
quipp ed, that \FORTRAN programs can b e written in any language."
1.2.2 An Example from Computer Languages
The relationship we noted b etween language and thought for natural human languages is
even more pronounced in the realm of arti cial computer languages. That is, the language
in which a programmer thinks a problem will b e solved will color and alter, in a basic
fundamental way, the fashion in which an algorithm is develop ed.
An example will help illustrate this relationship b etween computer language and problem
solution. Several years ago a studentworking in genetic researchwas faced with a task
involved in the analysis of DNA sequences. The problem could b e reduced to a relatively
simple form. The DNA is represented as a vector of N integer values, where N is very large
on the order of tens of thousands. The problem was to discover whether any pattern of
length M, where M was a xed and small constant say ve or ten is ever rep eated in the
vector of values.
ACTCGGATCTTGCATTTCGGCAATTGGACCCTGACTTGGCCA ...
The programmer dutifully sat down and wrote a simple and straightforward FORTRAN
program, something like the following:
DO 10 I = 1, N-M
DO 10 J = 1, N-M
FOUND = .TRUE.
DO 20 K = 1, M
20 IF X[I+K-1] .NE. X[J+K-1] THEN FOUND = .FALSE.
IF FOUND THEN ...
10 CONTINUE
4 CHAPTER 1. THINKING OBJECT ORIENTED
The studentwas somewhat disapp ointed when trial runs indicated his program would
need signi cantly many hours to complete. He discussed his problem with a second student,
who happ ened to b e pro cient in the programming language APL. The second student
said that she would like to try writing a program for this problem. The rst studentwas
dubious. After all, FORTRAN was known to b e one of the most \ecient" programming
languages. It was compiled, after all, and APL was only interpreted. Therefore it was with
a certain amount of incredulity that he discovered the APL programmer was able to write
an algorithm that worked in a matter of minutes, not hours.
What the APL programmer had done was to rearrange the problem. Rather than work-
ing with a vector of N elements, she reorganized the data into a matrix with roughly N
rows and M columns:
A C T C G G p ositions 1 to M
C T C G G A p ositions 2 to M +1
T C G G A T p ositions 3 to M +2
C G G A T T p ositions 4 to M +3
G G A T T C p ositions 5 to M +4
G A T T C T p ositions 6 to M +5
. . .
T G G A C C
G G A C C C
. . .
The APL programmer then sorted this matrix byrows. If any pattern was rep eated,
then two adjacentrows in the sorted matrix would have identical values.
. . .
T G G A C C
T G G A C C
. . .
It was a trivial matter to check for this condition. The reason the APL program was
faster had nothing to do with the sp eed of APL versus FORTRAN, but was simply b ecause
2
the FORTRAN program employed an algorithm that was O M N , whereas the sorting
solution used by the APL programmer required approximately O M N log N op erations.
The p oint of this story is not to indicate that APL is in anyway a \b etter" programming
language than FORTRAN, but to ask whyitwas that the APL programmer was naturally
led to discover a b etter solution. The reason, in this case, is that lo ops are very dicult
to write in APL, whereas sorting is trivial; it is a built-in op erator de ned as part of the
language. Thus, b ecause the op eration is so easy to p erform, go o d APL programmers tend
to lo ok for novel applications for sorting. It is in this manner that the programming language
in which the solution is to b e written directs the programmer's mind to view the problem in one fashion or another.
1.2. LANGUAGE AND THOUGHT 5
1.2.3 Church's Conjecture and the Sapir-Whorf Hyp othesis
The assertion that the language in which an idea is expressed can in uence or direct a line
of thought is relatively easy to b elieve. It should b e noted, however, a stronger conjec-
ture, known in linguistics as the Sapir-Whorf hyp othesis, go es much further and remains
controversial [Pullum 91].
The Sapir-Whorf hyp othesis asserts that it may b e p ossible for an individual working
in one language to imagine thoughts or utter ideas which cannot in anyway b e translated,
cannot even b e understo o d, by individuals op erating in a di erent linguistic framework.
According to advo cates of the hyp othesis, this can o ccur when the language of the second
individual has no equivalentwords, and lacks even concepts or categories for the ideas
involved in the thought. It is interesting to compare this idea with an almost directly
opp osite concept from computer science, namely Church's conjecture.
Starting in the 1930's and on through the 40's and 50's there was a great deal of
interest within the mathematical and nascent computing communityinavariety of for-
malisms that could b e used for the calculation of functions. Examples include the notations
prop osed byChurch [Church 36], Post [Post 36], Markov [Markov 51], Turing [Turing 36],
Kleene [Kleene 36] and others. Over time a numb er of arguments were put forth to demon-
strate that many of these systems could b e used in the simulation of others. Often, for a
pair of systems, such arguments could b e made in b oth directions; e ectively showing that
the systems were identical in computation p ower. The sheer numberofsuch arguments lead
the logician Alonzo Church to pronounce a conjecture that is now asso ciated with his name:
Church's Conjecture: Any computation for which there exists an e ective
pro cedure can b e realized bya Turing machine.
By nature this conjecture must remain unproven and unprovable, since wehaveno
rigorous de nition of the term \e ective pro cedure." Nevertheless, no counterexample has
yet b een found, and the weight of evidence seems to favor armation of this claim.
Acceptance of Church's conjecture has an imp ortant and profound implication for the
study of programming languages. Turing machines are wonderfully simple mechanisms,
and it do es not require many features in a language in order to b e able to simulate such
a device. In the 1960's, for example, it was demonstrated that a Turing machine could
be emulated in any language that p ossessed at least a conditional statement and a lo oping
construct [Bohm 66]. This greatly misundersto o d result was the ma jor ammunition used
to \prove" that the infamous goto statementwas unnecessary.
If we accept Church's conjecture, then any language in which it is p ossible to simulate
aTuring machine is suciently p owerful to p erform any realizable algorithm. To solve
a problem, nd the Turing machine that pro duces the desired result, whichbyChurch's
conjecture must exist, then simulate the execution of the Turing machine in your favorite
language. Thus, arguments ab out the relative\power" of programming languages, if by
power we mean \ability to solve problems," are generally vacuous. The late Alan Perlis had
6 CHAPTER 1. THINKING OBJECT ORIENTED
a term for such arguments, calling them a \Turing Tarpit," since they are often so dicult
to extricate oneself from, and so fundamentally p ointless.
Note that Church's conjecture is, in a certain sense, almost the exact opp osite of the
Sapir-Whorf hyp othesis. Church's conjecture states that in a fundamental fashion all pro-
gramming languages are identical. Any idea that can b e expressed in one language can, in
theory, b e expressed in any language. The Sapir-Whorf hyp othesis, you will recall, claimed
that it was p ossible for ideas to b e expressed in one language that could not b e expressed
in another.
Many linguists reject the Sapir-Whorf hyp othesis and instead adopt a sort of \Turing-
equivalence" for natural languages. By this we mean that, with a sucient amountofwork,
any idea can b e expressed in any language. For example, while the language sp oken bya
nativeofawarm climate may not make it instinctive to examine a eld a snow and categorize
it with resp ect to di erenttyp es or uses, with time and training it certainly can b e learned.
Similarly, ob ject-oriented techniques do not provide any new computational p ower which
p ermits problems to b e solved that cannot, in theory b e solved by other means. But ob ject-
oriented techniques do makeiteasier and more natural to address problems in a fashion
that tends to favor the management of large software pro jects.
Thus, for b oth computer and natural languages it is the case that the language will direct
thoughts, but cannot proscribe thoughts.
1.3 A New Paradigm
Ob ject-oriented programming is often referred to as a new programming paradigm. Other
programming paradigms sometimes mentioned include the imp erative-programming paradigm
languages suchasPascal or C, the logic programming paradigm Prolog, and the functional-
programming paradigm FP or Haskell.
It is interesting to examine the de nition of the word \paradigm": The following is from
the American Heritage Dictionary of the English Language:
par a digm n. 1. A list of all the in ectional forms of a word taken as
an illustrative example of the conjugation or declension to which it b elongs. 2.
Any example or mo del. [Late Latin parad gma, from Greek paradeigma, mo del,
from paradeiknunai, to compare, exhibit.]
At rst blush, the conjugation or declension of Latin words would seem to have little
to do with computer programming languages. To understand the connection, wemust note
that the word was brought backinto the mo dern vo cabulary through an in uential b o ok
called The Structure of Scienti c Revolutions, authored by the historian of science Thomas
Kuhn [Kuhn 70]. Kuhn used the term in the second form, to describ e a set of theories,
standards and metho ds that together representaway of organizing knowledge; that is, a
way of viewing the world. Kuhn's thesis was that revolutions in science o ccurred when an
older paradigm was reexamined, rejected and replaced by another.
1.4. A WAY OF VIEWING THE WORLD 7
It is in this sense, as a mo del or example, and as an organizational approach, that Rob ert
Floyd used the term in his 1979 ACM Turing Award lecture [Floyd 79], whichwas titled
\The Paradigms of Programming". A programming paradigm is a way of conceptualizing
what it means to p erform computation, and how tasks that are to b e carried out on a
computer should b e structured and organized.
Although new to computation, the organizing technique that lies at the heart of ob ject-
oriented programming can b e traced back through the history of science in general at least
as far as Linnus 1707-1778, if not even further back to the Greek philosopher Plato.
Paradoxically, the style of problem solving emb o died in the ob ject-oriented technique is
frequently the metho d used to address problems in everyday life. Thus, computer novices
are often able to grasp the basic ideas of ob ject-oriented programming easily, whereas those
p eople who are more computer literate are often blo cked by their own preconceptions. Alan
Kay, for example, found that it was usually easier to teach Smalltalk to children than to
computer professionals [Kay77].
In trying to understand exactly what is meantby the term object-orientedprogramming,
it is p erhaps useful to examine the idea from several alternative p ersp ectives. The next few
sections outline three di erent asp ects of ob ject-oriented programming; each illustrates a
particular reason why this technique should b e considered an imp ortant new to ol.
1.4 AWay of Viewing the World
To illustrate some of the ma jor ideas in ob ject-oriented programming, let us consider rst
howwe might go ab out handling a real-world situation, and then ask howwe could make
the computer more closely mo del the techniques employed.
Supp ose I wish to send some owers to my grandmother who is named Elsie for her
birthday. She lives in a city many miles away, so the p ossibilityofmy picking the owers
and carrying them myself to her do or is out of the question. Nevertheless, sending her the
owers is a task easy enough to do; I merely go down to my lo cal orist who happ ens to b e
named Flo, describ e the kinds and numb ers of owers I want sent, and my grandmother's
address, and I can b e assured the owers will b e delivered exp ediently and automatically.
1.4.1 Agents, Resp onsibility, Messages and Metho ds
At the risk of b elab oring a p oint, let me emphasize that the mechanism I used to solve
my problem was to nd an appropriate agent namely Flo, and to pass to her a message
containing my request. It is the responsibility of Flo to satisfy my request. There is some
method { that is, some algorithm or set of op erations { used by Flo to do this. I do not
need to know the particular metho d Flo will use to satisfy my request; indeed, often I do
not want to know the details. This information is usually hidden from my insp ection.
If I investigated, however, I might discover that Flo delivers a slightly di erent message
to another orist in my grandmother's city. That orist, in turn, makes the arrangement
8 CHAPTER 1. THINKING OBJECT ORIENTED
and passes it, along with yet another message, to a delivery p erson, and so on. In this
manner my request is nally satis ed by a sequence of requests from one agent to another.
So our rst principle of ob ject-oriented problem solving is the vehicle by which activities
are initiated:
Action is initiated in ob ject-oriented programming by the transmission of a
message to an agent an object resp onsible for the action. The message enco des
the request for an action, and is accompanied byany additional information ar-
guments needed to carry out the request. The receiver is the agent to whom the
message is sent. If the receiver accepts the message, it accepts the resp onsibility
to carry out the indicated action. In resp onse to a message, the receiver will
p erform some method to satisfy the request.
Wehave noted the imp ortant principle of information hiding in regard to message passing
{ that is, the client sending the request need not know the actual means by which the
request will b e honored. There is another principle, all to o human, that we can also observe
is implicit in message passing. That principle is, if there is a task to p erform, the rst
thought of the client is to nd someb o dy else whom it can ask to do the work. It is
interesting to note the degree to which this second reaction will often b ecome atrophied in
many programmers with extensive exp erience in using conventional techniques. Frequently,
a dicult hurdle to overcome is the idea in the programmers mind that he or she must
write everything themselves, and not use the services of others. An imp ortant part of
ob ject-oriented programming is the development of reusable comp onents, and an imp ortant
rst step in the use of reusable comp onents is a willingness to use an already develop ed
comp onent.
Information hiding is also an imp ortant asp ect of programming in conventional lan-
guages. In what sense is a message di erent from, say, a pro cedure call? In b oth cases,
there is a set of well-de ned steps that will b e initiated following the request.
There are two imp ortant distinctions. The rst is that in a message there is a designated
receiver for that message; the receiver is some agent to whom the message is sent. In a
pro cedure call, there is no designated receiver. Although we could adapt a convention of,
for example, always calling the rst argument to a pro cedure the receiver, something that
is very close to how receivers are actually implemented.
The second is that the interpretation of the message that is, the metho d used to resp ond
to the message is dep endent on the receiver, and can vary with di erent receivers. I can
give exactly the same message to my wife Beth, for example, and she will understand the
message and a satisfactory outcome will b e pro duced that is, owers will b e delivered to
my grandmother. However, the metho d Beth uses to satisfy the request in all likeliho o d,
simply passing the request on to Flo, will b e di erent from that p erformed by Flo in
resp onse to the same request. If I ask Ken, my dentist, to send owers to my grandmother,
I probably would b e making an error, since Ken may not have a metho d for solving that
problem. If he understo o d the request at all, he would probably issue an appropriate error diagnostic.
1.4. A WAY OF VIEWING THE WORLD 9
Let us move our discussion back to the level of computers and programs. There, the
distinction b etween message passing and pro cedure calling is that, in message passing, there
is a designated receiver, and the interpretation { that is, the selection of a metho d to execute
in resp onse to the message { mayvary with di erent receivers. Usually, the sp eci c receiver
for any given message will not b e known until run time, so the determination of which
metho d to invoke cannot b e made until then. Thus, wesay there is late binding between
the message function or pro cedure name and the co de fragment metho d used to resp ond
to the message. This situation is in contrast to the very early compile-time or link-time
binding of name to co de fragmentinconventional pro cedure calls.
1.4.2 Resp onsibilities
A fundamental concept in ob ject-oriented programming is to describ e b ehavior in terms of
responsibilities. My request for action indicates only the desired outcome owers for my
grandmother. The orist is free to pursue any technique that achieves the desired ob jective,
and is not hamp ered byinterference on my part.
By discussing a problem in terms of resp onsibilities we increase the level of abstraction.
This p ermits greater independence between agents, a critically imp ortant factor in solving
complex problems. In Chapter 2 we will investigate in more detail howwe can use an
emphasis on resp onsibility to drive the software design pro cess. The entire collection of
resp onsibilities asso ciated with an ob ject is often describ ed by the term protocol.
The di erence b etween viewing software in traditional structured terms and viewing
software in an ob ject-oriented p ersp ective can b e summarized byatwist on a well-known
quote:
\Ask not what you can do to your data structures,
but rather ask what your data structures can do for you"
1.4.3 Classes and Instances
Although I have only dealt with Flo a few times in the past, I have a rough idea of the
b ehavior I can exp ect when I go into her shop and present her with my request. I am able
to make certain assumptions b ecause I have some information ab out orists in general, and
I exp ect that Flo, b eing an instance of this category, will t the general pattern. We can
use the term Florist to represent the category or class of all orists. Let us incorp orate
these notions into our second principle of ob ject-oriented programming:
All ob jects are instances of a class. The metho d invoked by an ob ject in
resp onse to a message is determined by the class of the receiver. All ob jects of
a given class use the same metho d in resp onse to similar messages.
One current problem in the ob ject-oriented community is the proliferation of di erent
terms for similar ideas. Thus, a class is known in Ob ject Pascal as an object type, and a
10 CHAPTER 1. THINKING OBJECT ORIENTED
ancestor class whichwe will describ e shortly, is known as a superclass or parent class, and
so on. The glossary at the end of this b o ok should b e of some help with unusual terms. We
will use the convention, common in ob ject-oriented languages, of always designating classes
by a name b eginning with an upp ercase letter. Although commonly used, this convention
is not enforced by most language systems.
1.4.4 Class Hierarchies { Inheritance
There is more information I have ab out Flo { not necessarily b ecause she is a orist, but
b ecause she is a shopkeep er. I know, for example, that I probably will b e asked for money
as part of the transaction, and in return for payment I will b e given a receipt. These actions
are true of gro cers, of stationers, and of other shopkeep ers. Since the category Florist is a
more sp ecialized form of the category Shopkeep er,any knowledge I haveofShopkeep ers
is also true of Florists, and hence of Flo.
One way to think ab out howIhave organized my knowledge of Flo is in terms of a
hierarchy of categories see Figure 1.1. Flo is a Florist, but Florist is a sp ecialized form
of Shopkeep er.Furthermore, a Shopkeep er is also a Human; so I know, for example,
that Flo is probably bip edal. A Human is a Mammal therefore they nurse their young,
and a Mammal is an Animal therefore it breathes oxygen, and an Animal is a Material
Ob ject therefore it has mass and weight. Thus, there is quite a lot of knowledge I have
that is applicable to Flo that is not directly asso ciated with her, or even with her category
Florist.
The principle that knowledge of a more general category is applicable also to the more
sp eci c category is called inheritance.Wesay that the class Florist will inherit attributes
of the class or category Shopkeep er.
There is an alternative graphical technique that is often used to illustrate this relation-
ship, particularly when there are many individuals with di ering lineage's. This technique
shows classes listed in a hierarchical tree-like structure, with more abstract classes suchas
Material Ob ject or Animal listed near the top of the tree, and more sp eci c classes,
and nally individuals, listed near the b ottom. Figure 1.2 shows this class hierarchy for Flo.
This hierarchy also includes Beth, my dog Flash, Phyl the platypus who lives at the zo o,
and the owers themselves that I am sending to my grandmother.
Information that I p ossess ab out Flo b ecause she is an instance of class Human is also
applicable to my wife Beth, for example. Information that I have ab out her b ecause she
is a Mammal is applicable to Flash as well. Information ab out all memb ers of Material
Ob ject is equally applicable to Flo and to her owers. We capture this in the idea of
inheritance:
Classes can b e organized into a hierarchical inheritance structure. A child
class or subclass will inherit attributes from a parent class higher in the tree.
An abstract parent class is a class suchasMammal that is used only to create
sub classes, for which there are no direct instances.
1.4. A WAY OF VIEWING THE WORLD 11
' $
Material Object
' $
Animal
' $
Mammal
' $
Human
' $
Shopkeep er
$ '
Florist
Flo
&
&
&
&
&
& Figure 1.1: The categories surrounding Flo
12 CHAPTER 1. THINKING OBJECT ORIENTED
Material Objects
H
H
H
H
H
H
H
H
H
H
H
H
Animal Plant
Mammal Flower
X
X
X