Relationships in UML: Association, Aggregation, and Composition

Introduction

In the world of software design, everything is connected. Objects don’t exist in isolation — they interact, depend on, and manage each other. UML helps us visualize these connections through different types of relationships. Three of the most fundamental relationships in object-oriented design are Association, Aggregation, and Composition.

Understanding these relationships will help you model real-world scenarios more effectively and map them into Java code with clarity and precision.

Why UML Relationships Matter

  • They define how objects collaborate
  • They reflect object lifecycle dependencies
  • They clarify ownership and containment
  • They aid in designing loosely or tightly coupled systems

Relationship Overview

Diagram showing differences between Association, Aggregation, and Composition

1. Association

Association is the most basic relationship. It defines a general connection between two independent classes. Both objects can exist independently of each other.

Example: Student and Teacher

A student may be taught by multiple teachers, and each teacher can teach many students.

class Student {
    String name;
    List<Teacher> teachers;
}

class Teacher {
    String name;
    List<Student> students;
}

In UML, association is represented by a simple line connecting the two classes. Multiplicity (like 1..*, 0..1) is used to denote how many instances can be associated.

UML Association diagram between Student and Teacher

Key Traits of Association

  • Loosely coupled
  • No ownership implied
  • Objects can exist independently

2. Aggregation

Aggregation is a specialized form of association. It represents a "whole-part" relationship where the part can exist independently of the whole. It shows a has-a relationship.

Example: Department and Teachers

A department has teachers, but a teacher can exist independently (may teach in another department).

class Department {
    String name;
    List<Teacher> teachers;
}

class Teacher {
    String name;
}

In UML, aggregation is shown with a hollow diamond at the end of the whole (Department).

UML Aggregation between Department and Teacher

Key Traits of Aggregation

  • Represents whole-part relationship
  • Parts can exist independently
  • Shows shared ownership

3. Composition

Composition is a stronger form of aggregation. It implies ownership and a strict lifecycle dependency. If the whole is destroyed, the parts are also destroyed.

Example: School and ClassRooms

If a school is closed (deleted), its classrooms cease to exist — they have no meaning outside the school.

class School {
    String name;
    List<ClassRoom> classRooms;

    School() {
        classRooms = new ArrayList<>();
    }
}

class ClassRoom {
    String roomNumber;

    ClassRoom(String roomNumber) {
        this.roomNumber = roomNumber;
    }
}

In UML, composition is shown with a filled black diamond at the end of the whole (School).

UML Composition between School and ClassRoom

Key Traits of Composition

  • Represents strong ownership
  • Parts cannot exist without the whole
  • Destruction of whole leads to destruction of parts

Comparison Table

Relationship Direction Lifetime UML Notation Java Example
Association Bidirectional or Unidirectional Independent Simple Line Student - Teacher
Aggregation Whole to Part Independent Hollow Diamond Department - Teacher
Composition Whole to Part Dependent Filled Diamond School - ClassRoom

When to Use Each Relationship

Use Association when:

  • Objects are related but don’t depend on each other
  • You want flexible, loosely coupled classes

Use Aggregation when:

  • One object is a logical container for another
  • The contained object can exist outside the container

Use Composition when:

  • The part object cannot exist without the whole
  • You want to tightly couple the lifecycle

Practice Scenario: School System Design

Let’s map out relationships in a school:

  • Association: Student ↔ Teacher (they interact but live separately)
  • Aggregation: Department → Teacher (teacher can belong to multiple departments)
  • Composition: School → ClassRoom (classroom depends on school)
UML diagram combining Association, Aggregation and Composition in school context

Conclusion

Modeling relationships correctly is the backbone of good object-oriented design. UML’s ability to express Association, Aggregation, and Composition gives us the tools to mirror the real world — complete with ownership, lifecycle, and hierarchy.

By choosing the right relationship for your classes in both diagrams and Java code, you create designs that are not only accurate but also maintainable, scalable, and intuitive. As you continue modeling, practice recognizing these relationships in everyday systems. That’s the key to mastering UML at a professional level.

QUIZ

Question 1:What distinguishes composition from aggregation in UML relationships?

Question 2:In an association relationship, objects are loosely related and can exist independently.

Question 3:Which of the following are characteristics of aggregation?

Question 4:Consider a `Library` and `Book`. If a library owns the books and they should not exist outside it, what kind of relationship is most appropriate?

Question 5:A classroom having multiple students is best modeled using composition if students can't exist outside the class.

Question 6:Which UML concepts are directly associated with the representation of relationships?

Question 7:In the context of a school management system, which of these would most likely be an aggregation?

Question 8:Composition is a type of association.

Question 9:Which of these real-world examples correctly map to UML relationship types?

Question 10:Which of the following best describes the UML notation for composition?