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,131
- 1
- 20
- 25

- 4,794
- 1
- 14
- 13
-
18So is a car an aggregate or a composition of its parts? – reinierpost Mar 31 '11 at 12:13
-
2And how is aggregation different from any other relationship between two sorts of entities? – reinierpost Mar 31 '11 at 12:14
-
87@reinierpost In _reality_, a car is an aggregation of parts, and parts are simply an aggregation of molecules... However, in a _model_ it all depends on your requirements. Is it important to treat the engine as a separate entity so that you can track its lifetime independent of the car? Can you reuse the exact same engine in another car? If so, then you probably want aggregation. Otherwise you want a composition because you don't care about engines that aren't part of cars, nor do you care about reusing engines. – Curtis Batt Mar 31 '11 at 14:06
-
Your answer explains the difference between composition and aggregation, but not the difference between aggregation and some other kind of relationsip. – reinierpost Aug 05 '13 at 13:32
-
4what is missing is an implementation example for a complete understanding... – Chesnokov Yuriy Oct 26 '13 at 10:04
-
2What about employee when a company ceases to do business? Employee and people are different entities right? So can I say company is composition of employees? – arjun Jun 16 '15 at 02:13
-
@arjun, I think you are right. Employee and employer are the two participants of a legal relationship between a company and a person called employment. When the employment is ended (and sufficient time passes due to legal requirements) the Employee and Employer become irrelevant, while the person continues to exist. It is a bit more complicated than just aggregation vs composition. But again it depends on your use case. If you are just collecting employment statistics from many companies, you can have a composition because employees have no meaning outside of employers. You model the world... – Robert Cutajar Mar 25 '20 at 07:34
-
@Curtis Batt, in a real live a car own his parts (The Parts has no meaning or purpose in the system without the Car). The parts can exists independently (conceptually) from a car But a car can't be a car without his parts. If so than are the parts agregated or composed to a car? – Java bee Apr 19 '20 at 10:21
-
I get it: Composition is a specific kind of Aggregation – Java bee Apr 19 '20 at 11:19
-
This answer would be helpful if the examples had UML representation – Devin Gleason Lambert Jun 08 '20 at 20:35
-
The answer would be correct if you had not used the word `conceptually`. It's about life cycle of objects and ownership as you correctly pointed out later... – mip Jun 23 '21 at 18:45
-
The problem with arguing whether a Car is an aggregation or a composition of other objects is, it's a stupid example. Examples like Car, Engine, and Tire is good to teach Object Oriented concepts, but these examples fail quickly when we talk about more complex things. The examples given in this answer (Company/People/Accounts and TextEditor/Buffer/File) are far better examples. You can argue for Car and its part either way. – sampathsris Aug 19 '21 at 18:23
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
-
3In the example you quote the composition is a one-to-many and the aggregation also has a one-to-many relationship implied, though here it could also be a many-to-many relationship for the aggregation (we can suppose possible that a teacher can teach in multiple departments). Whereas a department cannot be part of multiple universities. Composition implies ownership whereas aggregation does not go beyond relationship. The quote is correct but the comment is not. – Newtopian Mar 24 '11 at 05:20
-
1it has nothing to do with destruction! UML does not define garbage collection system. – Display Name Mar 24 '11 at 10:01
-
2i think the wikipedia link is getting reflexive upvotes, but this is a terrible definition - as @bold pointed out these relationships have nothing to do with GC. This also falls apart when an object is the component of two other objects, such as the ball in a ball-joint joining two artificial limbs. The Component relationship is about functional dependence. – Steven A. Lowe Mar 24 '11 at 14:13
-
1I agree that my answer is severely lacking - but so is the WikiPedia article... – HorusKol Mar 24 '11 at 22:13
-
The difference between composition and aggregation is clear. The problem with aggregation is that it's unclear how it is different from ordinary association. – reinierpost Aug 16 '16 at 12:20
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);
}
}

- 107,706
- 45
- 295
- 310

- 311
- 3
- 3
-
6What is the difference between aggregation and association that is neither composition nor aggregation? – reinierpost Mar 24 '14 at 14:54
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;
}

- 836
- 10
- 17
-
this doesn't seem to offer anything substantial over points made and explained in prior 12 answers – gnat Jul 01 '15 at 07:05
-
-
Here the List of Employees is the part of Organization Object. How can this be aggregation ? – Salman Muhammad Ayub Feb 12 '16 at 09:59
-
-
I love this answer. It finally explained to me properly what the difference between aggregation and composition is. – PandasRocks Apr 01 '18 at 08:33
There is no single explanation. Different authors mean different things by aggregation. Most don't really mean anything specific by it.

- 587
- 6
- 8
-
5This is the correct answer. I've read it in two books, one of them being Martin Fowler's _UML Distilled_. – davidhaskins Mar 25 '11 at 12:52
-
5
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,808
- 2
- 84
- 151
-
This is nonstandard. The more standard distinction is that in composition, the members are always members of exactly one container. Tires can exist apart from cars, but if you only want to model tires on cars, the relationship becomes a composition. You will then still be able to change tires, but only as an atomic operation. – reinierpost Feb 01 '23 at 13:22
-
@reinierpost nonstandard, according to whom? Perhaps I misunderstood what you wrote but it sounds like by this definition, two spare tires in the trunk would be composition, as they are both members of one container. – Steven A. Lowe Feb 03 '23 at 08:19
-
You're right, 'standard' isn't really an appropriate term to use here. But in my limited experience, composition is defined by a restriction in cardinality on the relation and the rest is handwaving. – reinierpost Feb 03 '23 at 10:57
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
- 2
-
3this doesn't seem to add anything substantial over points made and explained in prior 11 answers – gnat Sep 05 '14 at 16:23
-
Respectfully disagree, @gnat. This is a helpful example of how the two could be implemented. People learn better with examples. (I came here to check my understanding that a pointer member was likely to be an aggregation, and an object member a composition. This is the only answer to directly address that.) – Bob Stein Apr 09 '19 at 17:40
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.