Background
In Object oriented programming a program is essentially multiple objects interacting with each other. Each interacting object have a relation. This relationship can be categorized as -
- Association,
- Aggregation and
- Composition
Difference between Association, Aggregation and Composition
For example you are related to your parent. In other words you are associated with your parent. This type of generic relationship is called association.
Aggregation and Composition are subsets of association meaning they are specific cases of association.
- In both aggregation and composition object of one class "owns" object of another class.
- But there is a subtle difference. In Composition the object of class that is owned by the object of it's owning class cannot live on it's own(Also called "death relationship"). It will always live as a part of it's owning object where as in Aggregation the dependent object is standalone and can exist even if the object of owning class is dead.
- So in composition if owning object is garbage collected the owned object will also be which is not the case in aggregation.
Confused? Lets take examples to understand Composition and Aggregation
- Composition Example :Consider example of a Heart and an Human. This type of relation
ship between Human and Heart class is called Composition. Object of Heart class cannot exist without
object of Human class and object of Heart has no
significance without Human class. To put in simple words Humanclass solely
"owns" the Heart class.
- Aggregation Example :Now consider class Car and class Wheel.
Car needs a Wheel object to function. Meaning Car object own Wheel
object but we cannot say Wheel object has no significance without Car
Object. It can very well be used in a Bike, Truck or different Cars
Object.
Summing it up
To sum it up association is a very generic term used to represent when on class used the functionalities provided by another class. We say it's composition if one parent class object owns another child class object and that child class object cannot meaningfully exist without the parent class object. If it can then it is called Aggregation.
Note : For Association you can either use an arrow depicted above or a simple line. In Argo UML (Wiki) tool which I use we can choose from either. For example above association diagram for Parent-Child can be