C

Core Java tutorial for beginners

Clean • Professional

Java - Association, Aggregation, and Composition (With Real-Life Examples)

4 minute

Association, Aggregation, and Composition in Java

In Object-Oriented Programming (OOP), classes often work together — objects interact and depend on each other.

These relationships between classes are known as Association, Aggregation, and Composition. They describe “how objects are related” and “how strongly one object depends on another.”


1. Association in Java

Association represents a relationship between two separate classes that are connected through their objects.

It shows how one object uses or interacts with another — but both objects can exist independently.

Example – Association

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

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

    void studyWith(Teacher t) {
        System.out.println(name + " studies with " + t.name);
    }
}

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

Output:

Aman studies with Mr. Sharma

Explanation:

[Student] ↔ [Teacher]
  (Both can exist independently)
  • They are linked only through an association (temporary relationship).
  • Neither owns nor depends on the other completely.

2. Aggregation in Java (Has-A Relationship)

Aggregation is a special form of association where one class contains a reference to another class, but both can still exist independently. It represents a “Has-A” relationship with weak ownership.

For example:

A Department has Teachers, but Teachers can exist even if the Department is deleted.

Example – Aggregation

class Department {
    String name;
    Department(String name) { this.name = name; }
}

class Teacher {
    String name;
    Department department; // Aggregation

    Teacher(String name, Department department) {
        this.name = name;
        this.department = department;
    }

    void showInfo() {
        System.out.println(name + " belongs to " + department.name + " department.");
    }
}

public class AggregationExample {
    public static void main(String[] args) {
        Department dept = new Department("Computer Science");
        Teacher t1 = new Teacher("Neha", dept);
        Teacher t2 = new Teacher("Ravi", dept);

        t1.showInfo();
        t2.showInfo();
    }
}

Output:

Neha belongs to Computer Science department.
Ravi belongs to Computer Science department.

Key Points of Aggregation

[Department]
      ↓
   [Teacher 1]
   [Teacher 2]
(Department has Teachers — but Teachers can exist independently)
  • Represents a “Has-A” relationship.
  • Weak dependency — contained objects can exist without the container.
  • Example: Department → Teacher, Library → Books, Company → Employees.

3. Composition in Java (Strong Association)

Composition is a stronger form of aggregation. Here, the lifetime of the contained object depends on the container object.

If the container (main object) is destroyed, contained objects are also destroyed.

Example – Composition

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

class Car {
    private Engine engine; // Composition

    Car() {
        engine = new Engine(); // Engine created inside Car
    }

    void startCar() {
        engine.start();
        System.out.println("Car is moving...");
    }
}

public class CompositionExample {
    public static void main(String[] args) {
        Car car = new Car();
        car.startCar();
    }
}

Output:

Engine starts...
Car is moving...

Explanation:

[Car] → [Engine]
(Engine exists only as part of Car — strong ownership)
  • The Car object owns the Engine object.
  • When the Car is destroyed, its Engine also ceases to exist.
  • This is strong ownershipComposition.

Difference Between Association, Aggregation, and Composition

FeatureAssociationAggregationComposition
DefinitionGeneral relationship between two classesWeak “Has-A” relationshipStrong “Has-A” relationship
DependencyObjects are independentContained object can exist without containerContained object cannot exist without container
OwnershipNo ownershipPartial ownershipFull ownership
LifetimeIndependentSeparate lifetimesShared lifetime (dependent)
ExampleStudent ↔ TeacherDepartment → TeacherCar → Engine

Real-World Examples

Real-World ObjectRelationship Type
Student ↔ LibraryAssociation
Company → EmployeesAggregation
Human → HeartComposition
Computer → CPUComposition

Points to Remember

  • Association: Relationship without ownership.
  • Aggregation: Weak ownership (“Has-A” but independent).
  • Composition: Strong ownership (object depends fully).
  • These relationships help design flexible, modular, and maintainable code.
Association     →  Student ↔ Library     (Independent)
Aggregation     →  Company → Employees   (Weak ownership)
Composition     →  Human → Heart         (Strong ownership)

 

Article 0 of 0