Logo
Published on

Association vs Aggregation vs Composition in Java & OOP - Complete Guide with Code and Examples

In Object-Oriented Programming, relationships between classes define how objects interact and depend on each other. The three most important types are:

  • Association
  • Aggregation
  • Composition

Understanding these distinctions is vital for object modeling, class design, and interview success.

1️⃣ What is Association?

Association is a general relationship between two classes where one object uses or interacts with another.

✅ Key Points:

  • Represents “uses-a” or “knows-a” relationship
  • Objects are loosely coupled
  • No ownership implied
  • Can be unidirectional or bidirectional

🛠️ Example:

class Employee {
    String name;
}

class Department {
    Employee employee; // Association
}

2️⃣ What is Aggregation?

Aggregation is a weaker form of Composition. It represents a “has-a” relationship where the contained object can exist independently of the container.

✅ Key Points:

  • Special case of Association
  • Denotes partial ownership
  • Object lifetime is independent
  • Weak coupling between classes

🛠️ Java Example:

class Student {
    String name;
}

class School {
    List<Student> students; // Aggregation
}

🧠 Explanation:

  • A Student can exist outside of the School
  • Removing the School does not destroy the Student objects

3️⃣ What is Composition?

Composition is a stronger form of Aggregation. It implies ownership where the contained object cannot exist without the container.

✅ Key Points:

  • Represents “has-a” with strong association
  • Contained object is fully dependent
  • When the container is destroyed, so is the contained object
  • Denotes tight coupling

🛠️ Java Example:

class Engine {
    void start() {
        System.out.println("Engine started");
    }
}

class Car {
    private Engine engine = new Engine(); // Composition

    void startCar() {
        engine.start();
    }
}

🧠 Explanation:

  • The Engine is part of the Car
  • If the Car is destroyed, so is the Engine

🌍 Real-World Analogies

Concept Analogy Example
Association Teacher–School: A teacher can work at a school but also elsewhere.
Aggregation Library–Books: Books can exist independently of the library.
Composition Human–Heart: A heart cannot function independently of the human body.

📊 Comparison Table

Feature Association Aggregation Composition
Relationship Type Uses-a / Knows-a Has-a (weak) Has-a (strong)
Ownership None Partial Full
Object Lifespan Independent Independent Dependent
Coupling Loose Weak Tight
Direction Uni or Bi-directional Mostly unidirectional Mostly unidirectional
Example Doctor & Patient University & Student House & Room

🔄 Diagrammatic Representation

Association:
[Doctor] → [Patient]

Aggregation:
[University] ◇── [Student]

Composition:
[Car] ◆── [Engine]

  • Arrow ----->: Association
  • Diamond <>: Aggregation
  • Filled diamond ◼️: Composition

🧠 When to Use What?

Use Case Use Relationship
Objects can exist independently Aggregation
Objects are tightly bound Composition
Objects only interact temporarily Association
Models whole-part with ownership Composition
One-to-one or one-to-many reference only Association/Aggregation

🎯 Key Interview Questions & Answers

Q1. What’s the difference between Aggregation and Composition? A: Aggregation allows independent existence of the contained object. Composition implies strong ownership — the contained object can't exist without the container.

Q2. Can an object have multiple relationships? A: Yes. For example, a Person may be associated with a Company (Association), own a Car (Composition), and borrow a Book from a Library (Aggregation).

Q3. Which is tighter: Aggregation or Composition? A: Composition is tighter. Aggregation is a weaker association.

Q4. Is inheritance a type of Association? A: No. Inheritance (is-a) defines hierarchy, while Association (uses-a) defines object interaction.

Q5. Can Composition exist without a constructor? A: Typically, Composition is initialized in the constructor or directly inline, but technically it can exist outside constructor too.

✅ Summary

Concept Definition Real Example
Association One class uses or interacts with another Doctor and Patient
Aggregation One class has another, but both can exist separately Library and Books
Composition One class owns another entirely Car and Engine

Understanding the nuances of object relationships is key to writing modular, scalable, and maintainable object-oriented code.

Whether you're preparing for interviews or designing a real-world application — mastering these concepts is essential for any Java or OOP developer.