Java Reference
In-Depth Information
Aggregation is a stronger form of dependency. If a class has objects of another class,
it certainly uses the other class. However, the converse is not true. For example, a
class may use the
Scanner
class without ever defining an instance field of class
Scanner
. The class may simply construct a local variable of type
Scanner
, or its
methods may receive
Scanner
objects as parameters. This use is not aggregation
because the objects of the class don't contain
Scanner
objectsÈŒthey just create or
receive them for the duration of a single method.
Generally, you need aggregation when an object needs to remember another object
between method calls.
You need to be able to distinguish the UML notations for inheritance, interface
implementation, aggregation, and dependency.
As you saw in
Chapter 8
, the UML notation for dependency is a dashed line with an
open arrow that points to the dependent class.
The arrows in the UML notation can get confusing.
Table 1
shows a summary of the
four UML relationship symbols that we use in this topic.
540
541
Table 1 UML Relationship Symbols
S
ELF
C
HECK
7.
Consider the
Bank
and
BankAccount
classes of
Chapter 7
. How are
they related?
8.
Consider the
BankAccount
and
SavingsAccount
objects of
Chapter 10
. How are they related?