Aggregation vs Composition

  • I understand what composition is in OOP, but I am not able to get a clear idea of what Aggregation is. Can someone explain?

  • Simple rules:

    1. A "owns" B = Composition : B has no meaning or purpose in the system without A
    2. 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.

    So is a car an aggregate or a composition of its parts?

    And how is aggregation different from any other relationship between two sorts of entities?

    @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.

    Your answer explains the difference between composition and aggregation, but not the difference between aggregation and some other kind of relationsip.

    what is missing is an implementation example for a complete understanding...

    What 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, 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...

    @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?

    I get it: Composition is a specific kind of Aggregation

  • From

    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

    In 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.

    it has nothing to do with destruction! UML does not define garbage collection system.

    i 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.

    I agree that my answer is severely lacking - but so is the WikiPedia article...

    The difference between composition and aggregation is clear. The problem with aggregation is that it's unclear how it is different from ordinary association.

    • 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)


    class Contained {
        public void disp() {
            System.out.println("disp() of Contained A");
    public class Container {
        private Contained c;
        Container() {
            c = new Contained(); 
        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();

    What is the difference between aggregation and association that is neither composition nor aggregation?

  • There is no single explanation. Different authors mean different things by aggregation. Most don't really mean anything specific by it.

    This is the correct answer. I've read it in two books, one of them being Martin Fowler's _UML Distilled_.

  • 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. enter image description here

    UML notations for different kind of dependency between two classes enter image description here

    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;   


    this doesn't seem to offer anything substantial over points made and explained in prior 12 answers

    Pretty sure books can exist without libraries. Bad example!

    Here the List of Employees is the part of Organization Object. How can this be aggregation ?

    How is association different from aggregation?

    I love this answer. It finally explained to me properly what the difference between aggregation and composition is.

  • 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!]

  • 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.

  • 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.

  • 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.

    this doesn't seem to add anything substantial over points made and explained in prior 11 answers

    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.)

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM