I understand what composition is in OOP, but I am not able to get a clear idea of what Aggregation is. Can someone explain?
9 Answers
Simple rules:
- A "owns" B = Composition : B has no meaning or purpose in the system without A
- A "uses" B = Aggregation : B exists independently (conceptually) from A
Example 1:
A Company is an aggregation of People. A Company is a composition of Accounts. When a Company ceases to do business its Accounts cease to exist but its People continue to exist.
Example 2: (very simplified)
A Text Editor owns a Buffer (composition). A Text Editor uses a File (aggregation). When the Text Editor is closed, the Buffer is destroyed but the File itself is not destroyed.
- 4,161
- 4,904
- 1
- 16
- 13
From http://en.wikipedia.org/wiki/Object_composition
Aggregation differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true. For example, a university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.
So - while you have an ownership relationship with composition the owned object is also destroyed when the owner is - an aggregation (and the objects contained) can exist independently.
--
Update: Apologies - this answer is far too simplistic in hindsight.
@Curtis Batt provides an excellent definition in his answer: Aggregation vs Composition
Composition is an Association
Aggregation is an Association
Composition is a strong Association (If the life of contained object totally depends on the container object, it is called strong association)
Aggregation is a weak Association (If the life of contained object doesn't depends on the container object, it is called weak association)
Example:
class Contained {
public void disp() {
System.out.println("disp() of Contained A");
}
}
public class Container {
private Contained c;
//Composition
Container() {
c = new Contained();
}
//Association
public Contained getC() {
return c;
}
public void setC(Contained c) {
this.c = c;
}
public static void main(String[] args) {
Container container = new Container();
Contained contained = new Contained();
container.setC(contained);
}
}
- 110,899
- 321
Composition(mixture) is a way to combine simple objects or data types into more complex ones. Compositions are a critical building block of many basic data structures
Aggregation(collection) differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true
╔═══════════╦═════════════════════════╦═══════════════════════╗
║ ║ Aggregation ║ Composition ║
╠═══════════╬═════════════════════════╬═══════════════════════╣
║ Life time ║ Have their own lifetime ║ Owner's life time ║
║ Relation ║ Has ║ part-of ║
║ Example ║ Car has driver ║ Engine is part of Car ║
╚═══════════╩═════════════════════════╩═══════════════════════╝
Both denotes relationship between object and only differ in their strength.
UML notations for different kind of dependency between two classes

Composition : Since Engine is part-of Car, relationship between them is Composition. Here is how they are implemented between Java classes.
public class Car {
//final will make sure engine is initialized
private final Engine engine;
public Car(){
engine = new Engine();
}
}
class Engine {
private String type;
}
Aggregation : Since Organization has Person as employees, relationship between them is Aggregation. Here is how they look like in terms of Java classes
public class Organization {
private List employees;
}
public class Person {
private String name;
}
- 916
There is no single explanation. Different authors mean different things by aggregation. Most don't really mean anything specific by it.
- 596
aggregation is a simple collection, like a bag of marbles
composition implies internal/functional dependencies, like the hinges on a box
cars aggregate passengers; they get in and out without breaking the car's functionality
the tires are components; remove one and the car no longer functions correctly
[note: the spare tire is an aggregate!]
- 33,828
I always look at composition as 'needs a', i.e. a car needs an engine, and I look at aggregation as 'things related for a purpose'. So staying with the car analogy, my aggregation may be to represent a journey which may involve bringing a car and passengers together. The journey does not own the car or the passengers, I'm aggregating data that is related for a specific scenario. When the journey is completed the car and the passengers go on. When a car is ended, the car and it's engine are normally destroyed together.
- 381
- 2
- 6
How about this simple example:
An array of objects is a composition. An array of pointers to objects is an aggregation.
If I delete the first one, its contents vanish with it. The second one, on the other hand can vanish without affecting its members existence unless there is a specific method that deletes each object as its pointer is deleted.
- 41
Semantically, all sets are made of subsets, right? Therefore:
The aggregation is when those subsets exists independently of the father set. As a monitor can be unplugged from the computer to be connected to another.
The composition is when those subsets depends of the existence of the father set. As a leaf is a part of a tree or liver is a part of a body.
These concepts talks about the kind of dependency between two objects or classes, conceptually. Directly in a program, in an aggregation, when the father object disposes, the aggregate objects should be disposed too. In the same scenario for a composition, composite son objects will persist then the father object dispenses.