Association vs Aggregation vs Composition

Understanding the nuances between Association, Aggregation, and Composition is essential for designing robust, scalable object-oriented systems. These relationships define how classes interact with each other β€” think of them as the “glue” in your application architecture.


πŸ”— Java Core Concept: Association vs Aggregation vs Composition


🧠 What Are These?

These are all forms of “has-a” relationships between objects, but they differ in strength of connection and lifecycle dependency.

ConceptType of RelationshipLifespan DependencyOwnership
Association“uses-a” / “has-a”IndependentNo
AggregationWhole–Part (loose)IndependentShared
CompositionWhole–Part (strong)DependentExclusive

1️⃣ Association

Definition: A general relationship between two classes where one class uses or references another.

  • No ownership.
  • Both can exist independently.
  • Often implemented via a field, method parameter, or local variable.

πŸ“¦ Example:

class Driver {
    String name;

    Driver(String name) {
        this.name = name;
    }

    void drive(Car car) {
        System.out.println(name + " is driving the car.");
    }
}

class Car {
    String model;

    Car(String model) {
        this.model = model;
    }
}

πŸ” Notes:

  • Driver and Car have an association.
  • Driver uses a Car, but doesn’t own or control its lifecycle.

2️⃣ Aggregation

Definition: A specialized form of Association where one class has another, but they can live independently.

  • Represents a “whole-part” relationship.
  • Weak ownership: Parts can belong to multiple wholes.

πŸ“¦ Example:

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

    Department(String name, List<Teacher> teachers) {
        this.name = name;
        this.teachers = teachers;
    }
}

class Teacher {
    String name;

    Teacher(String name) {
        this.name = name;
    }
}

πŸ” Notes:

  • Department aggregates Teachers.
  • If a department is deleted, the teachers still exist.
  • One teacher could belong to multiple departments.

3️⃣ Composition

Definition: A strong form of Aggregation where the child object cannot exist without the parent.

  • Strong ownership.
  • Lifecycles are tightly coupled.

πŸ“¦ Example:

class House {
    private Room room;

    House() {
        this.room = new Room();
    }

    void show() {
        room.describe();
    }
}

class Room {
    void describe() {
        System.out.println("This is a room in the house.");
    }
}

πŸ” Notes:

  • Room exists only inside House.
  • If House is destroyed, Room goes with it.
  • You compose objects to build complex ones.

πŸ“Œ Visual Metaphor

RelationMetaphor
AssociationA person uses a bike.
AggregationA university has students. If the university shuts down, students still exist.
CompositionA body has a heart. If the body dies, the heart is no longer useful alone.

🧠 Why It Matters

Understanding these distinctions helps you:

  • Design better data models
  • Avoid memory leaks and tight coupling
  • Clearly define ownership, responsibility, and object lifecycles

🏠 Homework for Students

πŸ“Œ Tasks:

  1. Association:

    • Create Customer and Order classes where:
      • Customer has a method placeOrder(Order order)
    • Show that Customer and Order are independent.
  2. Aggregation:

    • Create a Library class and a Book class.
    • A library has many books, but books can exist without a library.
    • Delete the library object β€” books should remain.
  3. Composition:

    • Create a Computer class composed of CPU and RAM classes.
    • CPU and RAM should only be created within Computer’s constructor.
    • Prove they cannot exist independently.

Ready to move into Static Keyword, Final Keyword, or start OOP design patterns like Factory Method or Singleton?