Java Reference
In-Depth Information
An aggregation relationship is usually represented as a data field in the aggregating class.
For example, the relationship in Figure 10.6 can be represented as follows:
public class
Name {
...
public class
Student {
private
Name name;
private
Address address;
public class
Address {
...
}
}
...
}
Aggregated class
Aggregating class
Aggregated class
Aggregation may exist between objects of the same class. For example, a person may have a
supervisor. This is illustrated in Figure 10.7.
1
Person
1
Supervisor
F
IGURE
10.7
A person may have a supervisor.
In the relationship “a person has a supervisor,” as shown in Figure 10.7, a supervisor can
be represented as a data field in the
Person
class, as follows:
public class
Person {
// The type for the data is the class itself
private
Person supervisor;
...
}
If a person can have several supervisors, as shown in Figure 10.8a, you may use an array to
store supervisors, as shown in Figure 10.8b.
1
public class
Person {
...
private
Person[] supervisors;
Person
Supervisor
m
}
(a)
(b)
F
IGURE
10.8
A person can have several supervisors.
Note
Since aggregation and composition relationships are represented using classes in the
same way, many texts don't differentiate them and call both compositions. We will do
the same in this topic for simplicity.
aggregation or composition
✓
✓
Check
10.10
What is an aggregation relationship between two objects?
10.11
What is a composition relationship between two objects?
Point