Aggregation - Class Relationship

Last Updated : 30 Aug, 2025

Aggregation is a specialized form of association that represents a "whole-part" relationship. It denotes a stronger relationship where one class (the whole) contains or is composed of another class (the part).
Unlike composition, the parts are not strongly dependent on the whole; they can exist independently even if the whole is removed.

111

A common way to describe this is through a “has-a” relationship. For example, a library has books, but books exist even without the library. The same book can also belong to another collection, highlighting the independence of the part.

In UML, aggregation is shown with a hollow diamond at the whole side, connected to the part. Multiplicity is often applied to indicate how many parts a whole may contain, such as a library holding 0..* books.

Understanding Aggregation using an example:

The company can be considered as the whole, while the employees are the parts. Employees belong to the company, and the company can have multiple employees. However, if the company ceases to exist, the employees can still exist independently.

aggregation
C++
#include <iostream>
#include <string>

class Teacher {
public:
    std::string name;
    Teacher(const std::string& n) : name(n) {}
};

class Department {
private:

    // Aggregation: Department refers to Teacher
    Teacher* teacher;  

public:
    Department(Teacher* t) : teacher(t) {}
    void showTeacher() const {
        if (teacher)
            std::cout << "Teacher: " << teacher->name << std::endl;
    }
};

int main() {
    Teacher t("Dr. Geek");
    Department dept(&t);
    dept.showTeacher();
    return 0;
}
Java
class Teacher {
    public String name;
    Teacher(String n) {
        name = n;
    }
}

class Department {
    // Aggregation: Department refers to Teacher
    private Teacher teacher;

    Department(Teacher t) {
        teacher = t;
    }

    public void showTeacher() {
        if (teacher != null) {
            System.out.println("Teacher: " + teacher.name);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Teacher t = new Teacher("Dr. Sharma");
        Department dept = new Department(t);
        dept.showTeacher();
    }
}
Python
class Teacher:
    def __init__(self, n):
        self.name = n


class Department:
    # Aggregation: Department refers to Teacher
    def __init__(self, t):
        self.teacher = t

    def showTeacher(self):
        if self.teacher:
            print("Teacher:", self.teacher.name)


def main():
    t = Teacher("Dr. Sharma")
    dept = Department(t)
    dept.showTeacher()


if __name__ == "__main__":
    main()
JavaScript
class Teacher {
    constructor(n) {
        this.name = n;
    }
}

class Department {
    // Aggregation: Department refers to Teacher
    constructor(t) {
        this.teacher = t;
    }

    showTeacher() {
        if (this.teacher) {
            console.log("Teacher: " + this.teacher.name);
        }
    }
}

// Example usage
const t = new Teacher("Dr. Sharma");
const dept = new Department(t);
dept.showTeacher();

Output
Teacher: Dr. Geek

Association vs Aggregation

  • Association is a simple connection between classes, while Aggregation is a special form of association that shows a whole–part relationship.
  • In Association, neither class owns the other, whereas in Aggregation the whole has a weak ownership of its parts.
  • Association links classes that can exist independently, and Aggregation also allows parts to exist independently, but within the context of a container (whole).
  • A Teacher–Student relationship is an example of Association, while a Library–Books relationship is an example of Aggregation.
Comment
Article Tags:

Explore