Encapsulation, Inheritance, and Polymorphism

20.2.5 Encapsulation, Inheritance, and Polymorphism

Although the structure and terminology introduced in Sections 20.2.1 through 20.2.4 differentiate OO systems from their conventional counterparts, three characteris- tics of object-oriented systems make them unique. As we have already noted, the OO class and the objects spawned from the class encapsulate data and the opera- tions that work on the data in a single package. This provides a number of impor- tant benefits:

• ? The internal implementation details of data and procedures are hidden from

What are the

primary

the outside world (information hiding). This reduces the propagation of side

benefits of an OO

effects when changes occur.

architecture?

• Data structures and the operations that manipulate them are merged in a sin- gle named entity—the class. This facilitates component reuse.

• Interfaces among encapsulated objects are simplified. An object that sends a message need not be concerned with the details of internal data structures. Hence, interfacing is simplified and the system coupling tends to be reduced.

Inheritance is one of the key differentiators between conventional and OO sys- tems. A subclass Y inherits all of the attributes and operations associated with its superclass, X. This means that all data structures and algorithms originally designed

CHAPTER 20

OBJECT-ORIENTED CONCEPTS AND PRINCIPLES

and implemented for X are immediately available for Y—no further work need be done. Reuse has been accomplished directly.

Any change to the data or operations contained within a superclass is immedi- ately inherited by all subclasses that have inherited from the superclass. 2 Therefore, the class hierarchy becomes a mechanism through which changes (at high levels) can be immediately propagated through a system.

It is important to note that, at each level of the class hierarchy, new attributes and operations may be added to those that have been inherited from higher levels in the hierarchy. In fact, whenever a new class is to be created, the software engineer has

a number of options: • The class can be designed and built from scratch. That is, inheritance is not

used. • The class hierarchy can be searched to determine if a class higher in the hier-

archy contains most of the required attributes and operations. The new class inherits from the higher class and additions may then be added, as required.

• The class hierarchy can be restructured so that the required attributes and operations can be inherited by the new class.

• Characteristics of an existing class can be overridden and private versions of attributes or operations are implemented for the new class.

To illustrate how restructuring of the class hierarchy might lead to a desired class, “Whereas an object is

consider the example shown in Figures 20.8. The class hierarchy illustrated in Figure

a concrete entity 20.8A enables us to derive classes X3 and X4 with characteristics 1, 2, 3, 4, 5 and 6 that exists in time

and 1, 2, 3, 4, 5, and 7, respectively. 3 Now, suppose that a new class with only char- and space, a class

represents only an acteristics 1, 2, 3, 4, and 8 is desired. To derive this class, called X2b in the example, abstraction, the

the hierarchy may be restructured as shown in Figure 20.8B. It is important to note ‘essence’ of an

that restructuring the hierarchy can be difficult, and for this reason, overriding is some- object, as it were.”

times used.

Grady Booch

In essence, overriding occurs when attributes and operations are inherited in the normal manner but are then modified to the specific needs of the new class. As Jacob- son notes, when overriding is used “inheritance is not transitive” [JAC92].

In some cases, it is tempting to inherit some attributes and operations from one class and others from another class. This is called multiple inheritance, and it is con- troversial. In general, multiple inheritance complicates the class hierarchy and cre- ates potential problems in configuration control (Chapter 9). Because multiple inheritance sequences are more difficult to trace, changes to the definition of a class that resides high in the hierarchy may have an unintended impact on classes defined lower in the architecture.

2 The terms descendants and ancestors [JAC92] are sometimes used to replace subclass and super- class, respectively. 3 For the purposes of this example, “characteristics” may be either attributes or operations.

X1 X1 Class

F I G U R E 20.8

hierarchy: original (A),

char1 restructured (B)

+ char4 + char5

+ char4 X2 X2

+ char5 + char8 + char6

char1 char1 char2

char2 char1

char3 char3 char2

char1

char4 char4 char3

char2

char5 char8 char4

char7 (B)

Polymorphism is a characteristic that greatly reduces the effort required to extend an existing OO system. To understand polymorphism, consider a conventional appli-