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 valwe 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 ...

Get Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.