1.7 Aggregation

When building new classes from existing classes using aggregation, a composite object is built from other constituent objects that are its parts.

Java supports aggregation of objects by reference, since objects cannot contain other objects explicitly. The fields can only contain values of primitive data types or references to other objects. Each object of the CharStack class has a field to store the reference to an array object that holds the characters. Each stack object also has a field of primitive data type int to store the index value that denotes the top of stack. This is reflected in the definition of the CharStack class, which contains an instance variable for each of these parts. In contrast to the constituent objects whose references are stored in fields, the values of primitive data types are stored in the fields of the composite object. The aggregation relationship is depicted by the UML diagram in Figure 1.7, showing that each object of the CharStack class will have one array object of char associated with it.

Figure 1.7. Class Diagram Depicting Aggregation

graphics/01fig07.gif