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-
Parts
» The Concurrent Development Model
» SUMMARY Software engineering is a discipline that integrates process, methods, and tools for
» PEOPLE In a study published by the IEEE [CUR88], the engineering vice presidents of three
» THE PROCESS The generic phases that characterize the software process—definition, development,
» THE PROJECT In order to manage a successful software project, we must understand what can go
» METRICS IN THE PROCESS AND PROJECT DOMAINS
» Extended Function Point Metrics
» METRICS FOR SOFTWARE QUALITY
» INTEGRATING METRICS WITHIN THE SOFTWARE PROCESS
» METRICS FOR SMALL ORGANIZATIONS
» ESTABLISHING A SOFTWARE METRICS PROGRAM
» Obtaining Information Necessary for Scope
» An Example of LOC-Based Estimation
» QUALITY CONCEPTS 1 It has been said that no two snowflakes are alike. Certainly when we watch snow
» SUMMARY Software quality assurance is an umbrella activity that is applied at each step in the
» R diagram 1.4 <part-of> data model; data model <part-of> design specification;
» SYSTEM MODELING Every computer-based system can be modeled as an information transform using an
» Facilitated Application Specification Techniques
» Data Objects, Attributes, and Relationships
» Entity/Relationship Diagrams
» Hatley and Pirbhai Extensions
» Creating an Entity/Relationship Diagram
» SUMMARY Design is the technical kernel of software engineering. During design, progressive
» Data Modeling, Data Structures, Databases, and the Data Warehouse
» Data Design at the Component Level
» A Brief Taxonomy of Styles and Patterns
» Quantitative Guidance for Architectural Design
» Isolate the transform center by specifying incoming and outgoing
» SUMMARY Software architecture provides a holistic view of the system to be built. It depicts the
» The User Interface Design Process
» Defining Interface Objects and Actions
» D E S I G N E VA L U AT I O N
» Testing for Real-Time Systems
» Organizing for Software Testing
» Criteria for Completion of Testing
» The Transition to a Quantitative View
» The Attributes of Effective Software Metrics
» Architectural Design Metrics
» Component-Level Design Metrics
» SUMMARY Software metrics provide a quantitative way to assess the quality of internal product
» Encapsulation, Inheritance, and Polymorphism
» Identifying Classes and Objects
» The Common Process Framework for OO
» OO Project Metrics and Estimation
» Event Identification with Use-Cases
» SUMMARY Object-oriented analysis methods enable a software engineer to model a problem by
» Partitioning the Analysis Model
» Designing Algorithms and Data Structures
» Program Components and Interfaces
» SUMMARY Object-oriented design translates the OOA model of the real world into an
» Testing Surface Structure and Deep Structure
» Deficiencies of Less Formal Approaches 1
» What Makes Cleanroom Different?
» Design Refinement and Verification
» SUMMARY Cleanroom software engineering is a formal approach to software development that
» Structural Modeling and Structure Points
» Describing Reusable Components
» SUMMARY Component-based software engineering offers inherent benefits in software quality,
» Guidelines for Distributing Application Subsystems
» Middleware and Object Request Broker Architectures
» An Overview of a Design Approach
» Consider expert Web developer will create a complete design, but time and cost can be appropriate
» A Software Reengineering Process Model
» Reverse Engineering to Understand Data
» Forward Engineering for Client/Server Architectures
» SUMMARY Reengineering occurs at two different levels of abstraction. At the business level,
Show more