Last Updated: February 12, 2026
What if a relationship is so strong that the "part" is meaningless and cannot even exist without the "whole"?
This is the world of Composition. It represents the strongest form of "has-a" relationship, where the whole owns the parts and controls their lifecycle.
When you use composition, you're saying:
Composition is a special type of association that signifies strong ownership between objects. The “whole” class is fully responsible for creating, managing, and destroying the “part” objects. In fact, the parts cannot exist without the whole.
If the part makes no sense without the whole, use composition.
Imagine a House and its Rooms:
This is a textbook example of composition. The rooms are tightly bound to the house—not just logically, but in lifecycle and ownership as well.
In UML class diagrams, composition is represented by a filled diamond (◆) at the “whole” end of the relationship. This is in contrast to aggregation's hollow diamond (◊) and association's plain solid line.
The diagram shows two classes connected by composition:
LineItem objects. The 1 to * multiplicity means one order can contain many line items.*--) on the Order side is the UML notation for composition. It signals that Order is the "whole" and LineItem is the "part," and that the line items are owned by the order.A class can be composed of multiple other objects. A Car is composed of an Engine, a Transmission, and a Chassis. These are integral parts of the car. You don't take the engine out and share it between two cars simultaneously. If the car is scrapped, these parts are scrapped with it.
In the software model, the Car creates these components and controls their lifecycle.
The filled diamonds on the Car side tell you: Car creates and owns these components. They don't float around the system independently.
Let's model the ordering scenario. An Order composes multiple LineItem objects. The order creates line items internally when items are added, and destroys them when the order is destroyed.
Pay attention to three things that make this composition:
addItem() method takes raw data (product name, quantity, price) and internally creates a new LineItem(...). The line items are not passed in from outside. This is the key structural difference from aggregation, where parts are created externally and passed into the whole.LineItem floating around in the system outside of an Order. No other class holds a reference to these line items. They are born inside the order and die with the order.Order object is garbage collected (or goes out of scope in C++), all its LineItem objects are destroyed too. No orphaned line items, no cleanup code, no dangling references.This is a true composition relationship: the parts exist only within the context of the whole, and their lifecycle is completely controlled by it.
Use composition when you can answer "yes" to these questions:
Composition is a preferred alternative to inheritance when building flexible systems.
“Favor composition over inheritance.” — GoF Design Principle
For example:
Vehicle can compose an Engine interface.PetrolEngine, ElectricEngine, or HybridEngine at runtime.This leads to cleaner, testable, and decoupled code.
Let’s compare association, aggregation, and composition side-by-side to understand how they differ in ownership, lifecycle, reusability, and usage in real systems.