We will explain the definitions and difference between the terms association, aggregation and composition in programming world in this page. These three terms come from object-oriented design and UML. They all describe relationships between classes, but differ in ownership and lifecycle dependency.
First we will show their usage in code, then we will explain their meanings in detail. For two objects, Foo and Bar the relationships between these two objects can be defined as:
Association
I have a relationship with an object. Foo uses Bar.
public class Foo
{
void Bar(Bar bar)
{
}
};
A basic connection between two classes where one uses or interacts with another.
Composition
I own an object and I am responsible for its lifetime, when Foo dies, so does Bar
public class Foo
{
private Bar bar = new Bar();
}
A stronger form of aggregation where the contained objects cannot exist without the owner.
In other words, A specialized form of association where one object contains others, but they can still exist independently.
Aggregation
I have an object which I've borrowed from someone else. When Foo dies, Bar may live on.
public class Foo
{
private Bar bar;
Foo(Bar bar)
{
this.bar = bar;
}
}
Definitions of association, aggregation and composition
Association is a relationship where all objects have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.
Aggregation is a specialised form of Association where all objects have their own lifecycle but there is ownership and child objects can not belong to another parent object. Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department teacher object will not be destroyed. We can think about it as a “has-a” relationship.
Composition is again specialised form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have it's lifecycle and if parent object is deleted all child objects will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belong to two different houses. If we delete the house - room will automatically be deleted. Let’s take another example relationship between Questions and options. Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will automatically be deleted.
Simple way to remember
• Association → just connected
• Aggregation → has-a (but can live separately)
• Composition → owns (cannot live separately)
Practical note
In real code (C#, C++, Java), the distinction is conceptual, not enforced by the language. For example:
• Aggregation → often references or pointers
• Composition → often objects created and destroyed inside the owner
Bottom line
The difference is about how strongly objects depend on each other:
• Association: loose connection
• Aggregation: container-like relationship
• Composition: ownership with lifecycle control