Aggregation in Java with Example

Introduction

Aggregation is a type of association that represents a "has-a" relationship with a whole-part hierarchy. In aggregation, the child object can exist independently of the parent object, implying a weak relationship between the parent and child. Aggregation allows one class to contain another class without owning its lifecycle.

Table of Contents

  1. What is Aggregation?
  2. Benefits of Aggregation
  3. Aggregation vs Composition
  4. Example: Aggregation in Java
  5. Real-World Examples of Aggregation
  6. Conclusion

1. What is Aggregation?

Aggregation is a special type of association that represents a whole-part relationship where the child (part) can exist independently of the parent (whole). It is used to model relationships where the contained objects are not strongly dependent on the lifecycle of the container object.

2. Benefits of Aggregation

  • Reusability: Aggregated objects can be reused across different parts of the application.
  • Flexibility: Aggregated objects can exist independently of the parent object, providing flexibility in design.
  • Modularity: Aggregation promotes modularity by separating the responsibilities of different classes.
  • Maintainability: Changes to the aggregated object do not necessarily affect the parent object, enhancing maintainability.

3. Aggregation vs Composition

FeatureAggregationComposition
RelationshipWhole-part (has-a)Whole-part (has-a)
DependencyChild can exist independently of the parentChild cannot exist independently of the parent
LifecycleParent and child have independent lifecyclesParent and child have dependent lifecycles
CouplingLoosely coupledTightly coupled

4. Example: Aggregation in Java

Example:

Let's create a Department class that aggregates multiple Employee objects using aggregation.

Step 1: Define the Employee Class

public class Employee {
    private String name;
    private int id;

    public Employee(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }
}

Step 2: Define the Department Class

import java.util.ArrayList;
import java.util.List;

public class Department {
    private String name;
    private List<Employee> employees;

    public Department(String name) {
        this.name = name;
        this.employees = new ArrayList<>();
    }

    public void addEmployee(Employee employee) {
        employees.add(employee);
    }

    public void showEmployees() {
        for (Employee employee : employees) {
            System.out.println("Employee ID: " + employee.getId() + ", Name: " + employee.getName());
        }
    }

    public String getName() {
        return name;
    }
}

Step 3: Main Class to Demonstrate Aggregation

public class Main {
    public static void main(String[] args) {
        Employee employee1 = new Employee("John Doe", 101);
        Employee employee2 = new Employee("Jane Smith", 102);

        Department department = new Department("IT Department");
        department.addEmployee(employee1);
        department.addEmployee(employee2);

        System.out.println("Department: " + department.getName());
        department.showEmployees();
        // Output:
        // Department: IT Department
        // Employee ID: 101, Name: John Doe
        // Employee ID: 102, Name: Jane Smith
    }
}

Explanation:

  • Employee: A simple class with name and id attributes.
  • Department: A class that uses aggregation to include multiple Employee objects. The Department class can add and display employees.
  • Main: A class to demonstrate the use of aggregation by creating Employee objects and adding them to the Department.

5. Real-World Examples of Aggregation

Example 1: School and Student

A School class can aggregate multiple Student objects. The Student objects can exist independently of the School.

Student Class

public class Student {
    private String name;
    private int rollNumber;

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

    public String getName() {
        return name;
    }

    public int getRollNumber() {
        return rollNumber;
    }
}

School Class

import java.util.ArrayList;
import java.util.List;

public class School {
    private String name;
    private List<Student> students;

    public School(String name) {
        this.name = name;
        this.students = new ArrayList<>();
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    public void showStudents() {
        for (Student student : students) {
            System.out.println("Student Roll Number: " + student.getRollNumber() + ", Name: " + student.getName());
        }
    }

    public String getName() {
        return name;
    }
}

Example 2: Library and Book

A Library class can aggregate multiple Book objects. The Book objects can exist independently of the Library.

Book Class

public class Book {
    private String title;
    private String author;

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }
}

Library Class

import java.util.ArrayList;
import java.util.List;

public class Library {
    private String name;
    private List<Book> books;

    public Library(String name) {
        this.name = name;
        this.books = new ArrayList<>();
    }

    public void addBook(Book book) {
        books.add(book);
    }

    public void showBooks() {
        for (Book book : books) {
            System.out.println("Title: " + book.getTitle() + ", Author: " + book.getAuthor());
        }
    }

    public String getName() {
        return name;
    }
}

6. Conclusion

Aggregation in Java is a powerful concept that allows classes to model a whole-part relationship where the parts can exist independently of the whole. This promotes modularity, reusability, and maintainability in the design of software systems. By understanding and using aggregation correctly, developers can create flexible and robust applications.

Happy coding!

Comments