Page 506 - Introduction to Programming with Java: A Problem Solving Approach
P. 506
472 Chapter 12 Aggregation, Composition, and Inheritance 12.1 Introduction
Prior to this chapter, the programs you’ve created have been relatively simple in terms of their object ori- entation, so you’ve been able to describe all the objects in a program with just a single class. But for more complex programs, you should consider implementing multiple classes, one for each different type of ob- ject within a program. In this chapter you’ll do just that, and you’ll focus on the different ways to organize classes in a multiple-class program. First, you’ll learn how to organize classes that are parts of a larger containing class. When classes are related like that, where one class is the whole and the other classes are parts of the whole, the classes form an aggregation. Then you’ll learn how to organize classes where one class, the base class, defines common features for a group of objects, and the other classes define special- ized features for each of the different types of objects in the group. When classes are related like that, the classes form an inheritance hierarchy. It’s called an inheritance hierarchy because the specialized classes inherit features from the base class.
In describing inheritance, we present various techniques for working with an inheritance hierarchy’s classes. Specifically, we present method overriding, which allows you to redefine a method in a specialized class that’s already been defined in the base class. We also present the final modifier, which allows you to prevent a specialized class from overriding a method defined in the base class.
As a follow-up to the initial presentation of aggregation and inheritance concepts, we describe how the
two design strategies can work together. It’s sometimes difficult to decide which is the best strategy to use.
To give you practice with those decisions, we guide you part way through a program design activity and de-
velop the skeleton for what could be a sophisticated card game program. In a final optional section, we show
you how to improve organization by creating an association class, which defines a set of characteristics that
Apago PDF Enhancer
belong to a particular relationship between classes.
By showing you how to organize multiple classes, this chapter provides you with important tools neces-
sary to tackle real-world problems. After all, most real-world programming projects are large and involve multiple types of objects. When you organize objects correctly, it makes programs easier to understand and maintain. And that’s good for everyone!
12.2 Composition and Aggregation
There are two primary forms of aggregation. As described above, standard aggregation is when one class is the whole and other classes are parts of the whole. The other form of aggregation also defines one class as the whole and other classes as parts of the whole. But it has an additional constraint that says the whole class is the exclusive owner of the parts classes. “Exclusive ownership” means that the parts classes cannot be owned by another class while they are being owned by the whole class. This exclusive-ownership form of aggregation is called composition. With composition, the whole class is called the composite, the parts classes are called components, and the composite contains the components. Composition is considered to be a strong form of aggregation since the composite-component connections are strong (due to each component having only one owner, the composite).
Composition and Aggregation in the Real World
The concept of composition was not created for computer programming; it’s frequently used for complex objects in the real world. Every living creature and most manufactured products are made up of parts. Of- ten, each part is a subsystem that is itself made up of its own set of subparts. Together, the whole system forms a composition hierarchy.