Docsity

Object Oriented Thinking - Lecture Notes | CS 582, Study notes of Computer Science

Partial preview of the text

Download Object Oriented Thinking - Lecture Notes | CS 582 and more Study notes Computer Science in PDF only on Docsity!

Part I

Understanding the

Ob ject-Oriented World View

14 CHAPTER 1. OBJECT-ORIENTED THINKING

ME Flora @ @ Sally's Florist

Wholesaler

Flower Arranger

E
E
E
E
E
E

Sally Delivery Person

Grower



Gardeners

C

C

Figure 1.1: The community of agents helping me

happ ens to b e named Flora), tell her the variety and quantity of owers I wish to send and give her Sally's address, and I can b e assured the owers will b e delivered exp ediently and automatically.

1.1.1 Agents and Communities

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, Flora) and to pass to her a message containing my request. It is the responsibility of Flora to satisfy my request. There is some method{some algorithm or set of op erations{used by Flora to do this. I do not need to know the particular metho d she 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 Flora delivers a slightly di erent message to another orist in my friend's city. That orist, in turn, p erhaps has a sub ordinate who makes the ower arrangement. The orist then passes the owers, along with yet another message, to a delivery p erson, and so on. Earlier, the orist in Sally's city had obtained her owers from a ower wholesaler who, in turn, had interactions with the ower growers, each of whom had to manage a team of gardeners.

So, our rst observation of ob ject-oriented problem solving is that the solution to my problem required the help of many other individuals (Figure 1.1). Without their help, my problem could not b e easily solved. We phrase this in a general fashion as the following:

An ob ject oriented program is structured as a community of interacting agents, called objects. Each ob ject has a role to play. Each ob ject provides a service, or p erforms an action, that is used by other memb ers of the community.

1.1. A WAY OF VIEWING THE WORLD 15

1.1.2 Messages and Metho ds

The chain reaction that ultimately resulted in the solution to my program b egan with my request to Flora. This request lead to other requests, which lead to still more requests, until my owers ultimately reached my friend. We see, therefore, that memb ers of this community interact with each other by making requests. So, our next 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 mes- sage to an agent (an object) resp onsible for the action. The message enco des the request for an action and is accompanied by any additional information (argu- ments) needed to carry out the request. The receiver is the ob ject 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.

We have 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 see is implicit in message passing. If there is a task to p erform, the rst thought of the client is to nd some- b o dy else he or she can ask to do the work. This second reaction often b ecomes atrophied in many programmers with extensive exp erience in conventional techniques. Frequently, a dicult hurdle to overcome is the idea in the programmer's mind that he or she must write everything 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 trust software written by others. 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. But, 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 ob ject to which the message is sent. In a pro cedure call, there is no designated receiver. 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 a message to my wife Elizab eth, for example, and she will understand it and a satisfactory outcome will b e pro duced (that is, owers will b e delivered to my friend). However, the metho d Elizab eth uses to satisfy the request (in all likeliho o d, simply passing the request on to Flora) will b e di erent from that used by Flora in resp onse to the same request. If I ask Kenneth, my dentist, to send owers to my friend, he may not have a metho d for solving that problem. If he understands the request at all, he will probably issue an appropriate error diagnostic.

1.1. A WAY OF VIEWING THE WORLD 17

Material Object

Animal

Mammal

Human

Shopkeep er

Florist Flora

Figure 1.2: { The categories surrounding Flora.

1.1.5 Class Hierarchies{Inheritance

I have more information ab out Flora{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 that in return for payment I will b e given a receipt. These actions are true of gro cers, stationers, and other shopkeep ers. Since the category Florist is a more sp ecialized form of the category Shopkeep er, any knowledge I have of Shopkeep ers is also true of Florists and hence of Flora.

One way to think ab out how I have organized my knowledge of Flora is in terms of a hierarchy of categories (see Figure 1.2). Flora 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 Flora is probably bip edal. A Human is a Mammal (therefore they nurse their young and have hair), and a Mammal is an Animal (therefore it breathes oxygen), and an Animal is a

18 CHAPTER 1. OBJECT-ORIENTED THINKING

Material Objects ^       Animal Plant

Mammal Flower

Dog Human Platypus

HH
HH
HH
HH
HH
HH

Shopkeep er Artist Dentist

XXXXXXXXXXXXX
X

Carnation

B
B
BB
HH
HH
HH
H

Florist Potter

Flash Flora Elizabeth Kenneth Phyl Sal ly's owers

Figure 1.3: { A class hierarchy for various material ob jects.

Material Object (therefore it has mass and weight). Thus, quite a lot of knowledge that I have that is applicable to Flora is not directly asso ciated with her, or even with her category Florist.

The principle that knowledge of a more general category is also applicable to a more sp eci c category is called inheritance. We say that the class Florist will inherit attributes of the class (or category) Shopkeep er.

There is an alternative graphical technique often used to illustrate this relationship, 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 (such as Material Object or Animal) listed near the top of the tree, and more sp eci c classes, and nally individuals, are listed near the b ottom. Figure 1.3 shows this class hierarchy for Flora. This

20 CHAPTER 1. OBJECT-ORIENTED THINKING

1.1.7 Summary of Ob ject-Oriented Concepts

Alan Kay, considered by some to b e the father of ob ject-oriented programming, identi ed the following characteristics as fundamental to OOP [Kay 1993 ]:

  1. Everything is an object.
  2. Computation is p erformed by ob jects communicating with each other, requesting that other ob jects p erform actions. Ob jects communicate by sending and receiving mes- sages. A message is a request for action bundled with whatever arguments may b e necessary to complete the task.
  3. Each ob ject has its own memory, which consists of other ob jects.
  4. Every ob ject is an instance of a class. A class simply represents a grouping of similar ob jects, such as integers or lists.
  5. The class is the rep ository for behavior asso ciated with an ob ject. That is, all ob jects that are instances of the same class can p erform the same actions.
  6. Classes are organized into a singly ro oted tree structure, called the inheritance hier- archy. Memory and b ehavior asso ciated with instances of a class are automatically available to any class asso ciated with a descendant in this tree structure.

1.2 Computation as Simulation

The view of programming represented by the example of sending owers to my friend is very di erent from the conventional conception of a computer. The traditional mo del describing the b ehavior of a computer executing a program is a process-state or pigeon-hole mo del. In this view, the computer is a data manager, following some pattern of instructions, wandering through memory, pulling values out of various slots (memory addresses), transforming them in some manner, and pushing the results back into other slots (see Figure 1.4). By examining the values in the slots, we can determine the state of the machine or the results pro duced by a computation. Although this mo del may b e a more or less accurate picture of what takes place inside a computer, it do es little to help us understand how to solve problems using the computer, and it is certainly not the way most p eople (pigeon handlers and p ostal workers excepted) go ab out solving problems. In contrast, in the ob ject-oriented framework we never mention memory addresses, vari- ables, assignments, or any of the conventional programming terms. Instead, we sp eak of ob jects, messages, and resp onsibility for some action. In Dan Ingalls's memorable phrase:

Instead of a bit-grinding pro cessor...plundering data structures, we have a uni- verse of well-b ehaved ob jects that courteously ask each other to carry out their various desires [Ingalls 1981 ].

1.2. COMPUTATION AS SIMULATION 21

a[1]: a[2]: a[3]: a[4]: 4 6 2 4

x: 47

i: j: 2 3

Figure 1.4: { Visualization of imp erative programming.

Another author has describ ed ob ject-oriented programming as \animistic": a pro cess of creating a host of help ers that form a community and assist the programmer in the solution of a problem [Actor 1987 ]. This view of programming as creating a \universe" is in many ways similar to a style of computer simulation called \discrete event-driven simulation." In brief, in a discrete event-driven simulation the user creates computer mo dels of the various elements of the simulation, describ es how they will interact with one another, and sets them moving. This is almost identical to the average ob ject-oriented program, in which the user describ es what the various entities in the universe for the program are, and how they will interact with one another, and nally sets them in motion. Thus, in ob ject-oriented programming, we have the view that computation is simulation [Kay 1977 ].

1.2.1 The Power of Metaphor

An easily overlo oked b ene t to the use of ob ject-oriented techniques is the p ower of metaphor. When programmers think ab out problems in terms of b ehaviors and resp onsibilities of ob- jects, they bring with them a wealth of intuition, ideas, and understanding from their ev- eryday exp erience. When envisioned as pigeon holes, mailb oxes, or slots containing values, there is little in the programmer's background to provide insight into how problems should b e structured. Although anthrop omorphic descriptions such as the quote by Ingalls may strike some p eople as o dd, in fact they are a re ection of the great exp ositive p ower of metaphor. Journalists make use of metaphor every day, as in the following description of ob ject-oriented programming from Newsweek:

Further Reading 23

 Ob jects and classes extend the concept of abstract data typ es by adding the notion of inheritance. Classes can b e organized into a hierarchical inheritance tree. Data and b ehavior asso ciated with classes higher in the tree can also b e accessed and used by classes lower in the tree. Such classes are said to inherit their b ehavior from the parent classes.

 Designing an ob ject oriented program is like organizing a community of individuals. Each memb er of the community is given certain resp onsibilities. The achivement of the goals for the community as a whole come ab out through the work of each memb er, and the interactions of memb ers with each other.

 By reducing the interdep endency among software comp onents, ob ject-oriented pro- gramming p ermits the development of reusable software systems. Such comp onents can b e created and tested as indep endent units, in isolation from other p ortions of a software application.

 Reusable software comp onents p ermit the programmer to deal with problems on a higher level of abstraction. We can de ne and manipulate ob jects simply in terms of the messages they understand and a description of the tasks they p erform, ignoring implementation details.

Further Reading

I said at the b eginning of the chapter that this is not a reference manual. The reference manual written by the develop ers of the language is [Gosling 96 ]. But p erhaps even more useful for most programmers is the annotated description of the Java class library pre- sented by [Chan 96 ]. Information on the internal workings of the Java system is presented by [Lindholm 97 ]. I noted earlier that many consider Alan Kay to b e the father of ob ject-oriented pro- gramming. Like most simple assertions, this one is only somewhat supp ortable. Kay himself [Kay 1993 ] traces much of the in uence on his development of Smalltalk to the earlier computer programming language Simula, develop ed in Scandinavia in the early 1960s [Dahl 1966 ]. A more accurate history would b e that most of the principles of ob ject- oriented programming were fully worked out by the develop ers of Simula, but that these would have b een largely ignored by the profession had they not b een rediscovered by Kay in the creation of the Smalltalk programming language. I will discuss the history of OOP in more detail in the next chapter. Like most terms that have found their way into the p opular jargon, object-oriented is used more often than it is de ned. Thus, the question What is ob ject-oriented programming? is surprisingly dicult to answer. Bjarne Stroustrup has quipp ed that many arguments app ear to b oil down to the following syllogism:

 X is go o d.

24 CHAPTER 1. OBJECT-ORIENTED THINKING

 Ob ject-oriented is go o d.

 Ergo, X is ob ject-oriented [Stroustrup 1988 ].

Roger King argued [Kim 1989 ], that his cat is ob ject-oriented. After all, a cat exhibits char- acteristic b ehavior, resp onds to messages, is heir to a long tradition of inherited resp onses, and manages its own quite indep endent internal state. Many authors have tried to provide a precise description of the prop erties a program- ming language must p ossess to b e called object-oriented. I myself have written an earlier b o ok ([Budd 97 ]) that tries to explain ob ject-oriented concepts in a language-indep ent fash- ion. See also, for example, the analysis by Josephine Micallef [Micallef 1988 ], or Peter Wegner [Wegner 1986 ]. Wegner, distinguishes object-based languages, which supp ort only abstraction (such as Ada), from object-oriented languages, which must also supp ort inheri- tance. Other authors{notably Brad Cox [Cox 1990 ]{de ne the term much more broadly. To Cox, ob ject-oriented programming represents the objective of programming by assembling solutions from collections of o -the-shelf sub comp onents, rather than any particular tech- nology we may use to achieve this ob jective. Rather than drawing lines that are divisive, we should embrace any and all means that show promise in leading to a new software Industrial Revolution. Cox's b o ok on OOP [Cox 1986 ], although written early in the development of ob ject-oriented programming and now somewhat dated in details, is nevertheless one of the most readable manifestos of the ob ject-oriented movement.

Study Questions

  1. What is the original meaning of the word paradigm?
  2. How do ob jects interact with each other?
  3. How are messages di erent from pro cedure calls?
  4. What is the name applied to describ e an algorithm an ob ject uses to resp ond to a request?
  5. Why do es the ob ject-oriented approach naturally imply a high degree of information hiding?
  6. What is a class? How are classes linked to b ehavior?
  7. What is a class inheritance hierarchy? How is it linked to classes and b ehavior?
  8. What do es it mean for one metho d to override another metho d from a parent class?
  9. What are the basic elements of the pro cess-state mo del of computation?