Java OOP · Module 12
Association, aggregation, composition
Three strengths of HAS-A, told apart by lifetime and ownership
What you'll be able to do
- Distinguish association, aggregation and composition in real code
- Read ownership from the constructor: created inside vs passed in
- Store a collection of parts and iterate over them
- Explain why the distinction matters for deletion and reuse
Association
Association: objects that simply know each other
The weakest link. Two objects are connected so they can collaborate, but neither owns the other and neither is a part of the other.
- A Teacher teaches Students; both exist perfectly well on their own.
- A Doctor treats Patients.
- An Order references a Customer.
It may be unidirectional (only one side holds the reference) or bidirectional (both do). Prefer unidirectional: two-way links must be kept in sync and are a classic source of bugs.
Aggregation
Aggregation vs composition: who owns the part?
A team is made of players, and a house is made of rooms. Delete the team and the players go home; delete the house and the rooms are gone forever.
Both are whole–part relationships. The difference is lifetime ownership, and in code you usually read it straight off the constructor:
- Aggregation — the part is passed in from outside: Team(Player[] players). The part existed before the whole and survives it. Also called a *weak* HAS-A.
- Composition — the whole creates the part: House() { this.rooms = new Room[...]; }. The part is born and dies with the whole. A *strong* HAS-A.
The practical consequences show up when things are deleted or shared: an aggregated part may be shared between several wholes; a composed part must not be handed out at all.
Read the constructor
Aggregation (weak)
part passed in
- Player exists before and after the Team
- The same Player may join another Team
- Deleting the Team deletes no players
- UML: hollow diamond
Composition (strong)
part created inside
- Rooms have no life outside the House
- They are never shared with another House
- Deleting the House deletes the rooms
- UML: filled diamond
Add the same Player to a second Team — legal, and a good demonstration of why this is aggregation rather than composition.
All four relationships at a glance
| Relationship | Java shape | Lifetime | Example |
|---|---|---|---|
| Inheritance (IS-A) | class B extends A | same object | SavingsAccount / Account |
| Association | field, no ownership | independent | Order / Customer |
| Aggregation | field, part passed in | part survives the whole | Team / Player |
| Composition | field, part created inside | part dies with the whole | House / Room |
In everyday conversation developers say 'composition' for all three HAS-A forms. Use the precise words when the lifetime actually matters — and in exams.
Composition leaks through getters
If House.getRooms() returns the internal array, callers can modify the rooms behind the house's back — encapsulation from Module 4 is broken. Return a copy, or expose only the operations you intend (house.addFurniture(...)).
Prove it — coding challenges
Common mistakes
Calling every HAS-A relationship 'composition' in an exam answer.Creating the part inside the constructor when callers must supply it.Storing parts in an array without tracking how many are used.Lesson playground
A playlist aggregates songs. Add a `remove` idea or a `total()` method — and note that the songs still exist if the playlist is thrown away.
Check understanding
Quiz
Question 1 of 5
One player, two teams. What is printed?
Summary
- Association: objects know each other, no ownership.
- Aggregation: whole–part, part passed in and outlives the whole.
- Composition: whole–part, part created inside and dies with the whole.
- Read the constructor to tell them apart, and never leak an internal collection.