Association, Composition, and Aggregation in Java

🔹Introduction

In Object-Oriented Programming (OOP), classes often need to work together. For example, a Car is made of Engine, Wheels, and Seats. Similarly, a University has Students and Departments.

In Java, these relationships are represented using:

  • Association
  • Composition
  • Aggregation

Understanding these relationships helps us design systems that are closer to real-world modeling and easier to maintain.


🔹1. Association in Java

Definition:
Association represents a relationship between two separate classes that are connected through their objects. It can be one-to-one, one-to-many, many-to-one, or many-to-many.

👉 In simple words:

“Association means two classes are related, but neither owns the other.”

✅ Example: Teacher and Student

A teacher can teach many students, and a student can learn from multiple teachers. Both can exist independently, but they share a relationship.

class Teacher {
    private String name;

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

    public String getName() {
        return name;
    }
}

class Student {
    private String name;

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

public class AssociationExample {
    public static void main(String[] args) {
        Teacher teacher = new Teacher("Mr. Sharma");
        Student student = new Student("Rahul");

        // Association - both exist independently
        System.out.println(student.getName() + " is taught by " + teacher.getName());
    }
}

🏆 Real-world use case:

  • Doctors and Patients
  • Employees and Departments
  • Bank and Customers

🔹2. Aggregation in Java (HAS-A Relationship)

Definition:
Aggregation is a special form of Association where one class contains a reference to another class. However, the contained object can exist independently of the container.

👉 In simple words:

“Aggregation means HAS-A relationship, but the lifecycles are independent.”

✅ Example: Department and Professor

A Department has Professors , but Professors can also work in other departments or exist even if the Department is deleted.

  • A Department may have several Professors.
  • If the Department is closed, the Professor objects can still exist independently (they may move to another Department).
  • Since a Professor can exist without being tied to a specific Department, this represents a weak association — also known as Aggregation.
import java.util.*;

class Professor  {
    String name;

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

class Department {
    String name;
    List<Professor> professors;

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

public class AggregationExample {
    public static void main(String[] args) {
        Professor p1 = new Professor("Mr. Sharma");
        Professor p2 = new Professor("Ms. Gupta");

        List<Professor> professors = new ArrayList<>();
        professors.add(p1);
        professors.add(p2);

        Department dept = new Department("Computer Science", professors);

        System.out.println("Department: " + dept.name);
        for (Professor p : dept.professors) {
            System.out.println("Professor: " + p.name);
        }
    }
}

🏆 Real-world use case:

  • Library and Books (Books can exist without Library)
  • Team and Players (Players can exist without a Team)
  • Company and Employees

🔹3. Composition in Java (Strong HAS-A Relationship)

Definition:
Composition is a stronger form of Aggregation where the contained object cannot exist without the container. If the container object is destroyed, the contained object is also destroyed.

👉 In simple words:

“Composition means HAS-A relationship with dependency. The child object cannot live without the parent object.”

✅ Example: House and Rooms

A Room cannot exist without a House. If the House is destroyed, Rooms are also gone.

  • A House consists of several Rooms.
  • If the House object is destroyed, all its Room objects will also be destroyed.
  • Since a Room cannot exist without its House, this represents a strong association — also known as Composition.
class Room {
    private String name;

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

    public String getName() {
        return name;
    }
}

class House {
    private Room room;

    House(String roomName) {
        this.room = new Room(roomName); // strong dependency
    }

    public Room getRoom() {
        return room;
    }
}

public class CompositionExample {
    public static void main(String[] args) {
        House house = new House("Living Room");
        System.out.println("House has a " + house.getRoom().getName());
    }
}

🏆 Real-world use case:

  • Car and Engine (Engine cannot exist without Car in this context)
  • Human and Heart (Heart cannot exist independently)
  • Laptop and Keyboard

🔹Key Differences: Association vs Aggregation vs Composition

FeatureAssociationAggregationComposition
TypeGeneral relationshipWeak HAS-AStrong HAS-A
DependencyObjects are independentContained object can exist without containerContained object cannot exist without container
LifecycleIndependentSeparate lifecycleDependent lifecycle
ExampleTeacher ↔ StudentDepartment → TeacherHouse → Room

🎯Summary

  • Association: Simple relationship, objects are independent.
  • Aggregation: HAS-A relationship, weaker, independent lifecycle.
  • Composition: Strong HAS-A, dependent lifecycle.

By choosing the correct relationship, you can design better, maintainable, and real-world-like applications in Java.

Backend developer working with Java, Spring Boot, Microservices, NoSQL, and AWS. I love sharing knowledge, practical tips, and clean code practices to help others build scalable applications.

Leave a Reply

Your email address will not be published. Required fields are marked *