OBJECT-ORIENTED CONCEPTS Any discussion of object-oriented software engineering must begin by addressing the

20.2 OBJECT-ORIENTED CONCEPTS Any discussion of object-oriented software engineering must begin by addressing the

term object-oriented. What is an object-oriented viewpoint? Why is a method con- “Object-oriented

sidered to be object-oriented? What is an object? Over the years, there have been programming is not

many different opinions (e.g., [BER93], [TAY90], [STR88], [BOO86]) about the correct so much a coding

answers to these questions. In the discussion that follows, we attempt to synthesize technique as it is a

the most common of these. code packaging

technique, a way for To understand the object-oriented point of view, consider an example of a real code suppliers to

world object—the thing you are sitting in right now—a chair. Chair is a member (the encapsulate

term instance is also used) of a much larger class of objects that we call furniture. A functionality for

set of generic attributes can be associated with every object in the class furniture. delivery to

customers.” For example, all furniture has a cost, dimensions, weight, location, and color, among

Brad Cox

many possible attributes. These apply whether we are talking about a table or a chair,

a sofa or an armoire. Because chair is a member of furniture, chair inherits all attrib- utes defined for the class. This concept is illustrated schematically in Figure 20.2.

Class: furniture Cost

The object inherits

Dimensions

all attributes of the class

Weight Location Color

Object: chair Cost

Dimensions Weight Location Color

F I G U R E 20.2

Inheritance from class to object

CHAPTER 20

OBJECT-ORIENTED CONCEPTS AND PRINCIPLES

Once the class has been defined, the attributes can be reused when new instances of the class are created. For example, assume that we were to define a new object called a chable (a cross between a chair and a table) that is a member of the class furniture. Chable inherits all of the attributes of furniture.

We have attempted an anecdotal definition of a class by describing its attributes, but something is missing. Every object in the class furniture can be manipulated in

a variety of ways. It can be bought and sold, physically modified (e.g., you can saw Data modeling notation

XRef

can be used to off a leg or paint the object purple) or moved from one place to another. Each of these represent objects and

operations (other terms are services or methods) will modify one or more attributes of their attributes. See

the object. For example, if the attribute location is a composite data item defined as Chapter 12 for details.

location = building + floor + room then an operation named move would modify one or more of the data items (building,

floor, or room) that form the attribute location. To do this, move must have "knowledge" of these data items. The operation move could be used for a chair or a table, as long as both are instances of the class furniture. All valid operations (e.g., buy, sell, weigh) for the class furniture are "connected" to the object definition as shown in Figure

20.3 and are inherited by all instances of the class.

Class: furniture Cost

Dimensions

The object inherits

Weight

all attributes and

Location

operations of the class

Color Buy

Sell Weigh Move

Object: chair Cost

Dimensions

Object: chable

Weight Location

Cost

Color

Dimensions Weight Location

Buy

Color

Sell Weigh Move

F I G U R E 20.3

Buy

Sell

Inheritance of

from class to

object

The object chair (and all objects in general) encapsulates data (the attribute val- ues that define the chair), operations (the actions that are applied to change the attrib- “Encapsulation

utes of chair), other objects (composite objects can be defined [EVB89]), constants prevents a program

(set values), and other related information. Encapsulation means that all of this infor- from becoming so

interdependent that mation is packaged under one name and can be reused as one specification or pro-

a small change has gram component. massive ripple

Now that we have introduced a few basic concepts, a more formal definition of effects.”

object-oriented will prove more meaningful. Coad and Yourdon [COA91] define the

Jim Rumbaugh

term this way:

et al.

object-oriented = objects + classification + inheritance + communication Three of these concepts have already been introduced. We postpone a discussion of

communication until later.