B3.2.2 · OOP for multiple classes
Aggregation & composition
By the end you can
Distinguish weak aggregation from strong composition whole-part relationships and interpret UML diamond notations.
While inheritance models an “is-a” relationship, most real-world software components are built by combining separate objects into a larger whole. This is the “has-a” relationship, and it comes in two distinct flavors: and .
1. Aggregation (Weak “has-a”)#
In aggregation, the component object exists independently of the container:
- Independent lifecycles: If you delete the container object, the component objects remain intact in memory.
- Example: A
CoursehasStudentobjects. If the course is canceled, the students do not vanish from existence; they simply enroll in other courses. - UML symbol: An open (hollow white) diamond at the container end of the association line.
2. Composition (Strong “has-a”)#
In composition, the container owns the lifecycle of its components:
- Coupled lifecycles: The component is created when the container is created, and when the container is destroyed, its components are deallocated immediately.
- Example: A
BuildinghasRoomobjects. A room cannot float in space without its building; if the building is demolished, all its rooms are demolished with it. - UML symbol: A filled (solid black) diamond at the container end of the association line.
Quick Reference: Inheritance vs Aggregation vs Composition#
| Relationship | Meaning | Example | UML Notation |
|---|---|---|---|
| Inheritance | is-a | Car is-a Vehicle | Hollow triangle arrow |
| Aggregation | has-a (independent) | Library has Books | Hollow white diamond |
| Composition | has-a (dependent) | Human has Heart | Solid black diamond |
Check yourself
Aggregation and composition
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 8 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Compare aggregation and composition in object-oriented design in terms of coupling strength, lifecycle dependency, and UML notation.
[4]Compare: Similarities and differences, about both things, point by point.
2.Distinguish between the 'is-a' relationship of inheritance and the 'has-a' relationship of composition or aggregation.
[2]Distinguish: Make the differences between two things clear.
3.Explain why the relationship between a Computer and its Motherboard is considered composition rather than aggregation.
[2]Explain: Give reasons: say what happens AND why, or how one thing causes another.
In a nutshell
- Both aggregation and composition model “has-a” (whole-to-part) relationships where one class contains another.
- Aggregation is a weak relationship where component objects have independent lifecycles (hollow diamond in UML).
- Composition is a strong relationship where the component cannot exist without its parent container (solid black diamond in UML).
- Favor composition over inheritance to create flexible, loosely coupled software architectures.
