Open links in new tab
  1. In Java, Aggregation is a special form of association that represents a HAS-A relationship between two classes. It occurs when one class contains a reference to another class, but both can exist independently. This makes aggregation a weaker relationship compared to composition, where the lifecycle of the contained object is tied to the container object.

    In aggregation:

    • The container (whole) and the contained (part) have independent lifecycles.

    • It is typically unidirectional — e.g., a Department has Students, but Students don’t necessarily know about the Department.

    • It is often used for code reusability and modular design.

    Example: Department and Students

    import java.util.*;

    class Student {
    private String name;
    private int id;

    public Student(String name, int id) {
    this.name = name;
    this.id = id;
    }
    public String getName() { return name; }
    }

    class Department {
    private String deptName;
    private List<Student> students;

    public Department(String deptName, List<Student> students) {
    this.deptName = deptName;
    this.students = students;
    }
    public List<Student> getStudents() { return students; }
    }

    class AggregationExample {
    public static void main(String[] args) {
    Student s1 = new Student("Alice", 1);
    Student s2 = new Student("Bob", 2);

    List<Student> csStudents = new ArrayList<>();
    csStudents.add(s1);
    csStudents.add(s2);

    Department csDept = new Department("Computer Science", csStudents);

    System.out.println("Department: " + csDept.getStudents().size() + " students");
    }
    }
    Copied!
  1. Java - Aggregation - Online Tutorials Library

    Learn what aggregation is in Java, how it differs from composition, and how to use it in your projects. See examples of aggregation with classes like Vehicle, Speed, Student and Address.

  2. Association, Composition and Aggregation in Java

    May 7, 2025 · Concepts of aggregation are quite important for developing modular and reusable software components. Aggregation is a type of …

  3. Aggregation (HAS-A relationship) in Java - Studytonight

    Learn what aggregation is and how to use it in Java with examples. Aggregation is a one-way relationship between two classes, where one class has a reference to another class.

  4. Composition, Aggregation, and Association in Java - Baeldung

    Learn the difference between composition, aggregation, and association in Java, and how to model them with UML and source code. See examples of real-life scen…

    Objects have relationships between them, both in real life and in programming. Sometimes it’s difficult to understand or implement these relationships. In this tutorial, we’ll focus on Java’s take on three sometimes easily mixed up types of r…
    See more on baeldung.com
    • Published: Aug 3, 2019
    • People also ask
    • Aggregation in Java - W3Schools

      Learn what aggregation is and how to use it in Java. Aggregation is a relation between two classes that is a has-a and whole/part relationship, and it is a form of association for code …

    • Aggregation in Java with Example

      Aggregation is a special type of association that represents a whole-part relationship where the child (part) can exist independently of the parent …

    • Understanding Aggregation in Java - javaspring.net

      Nov 12, 2025 · What is Aggregation in Java? Aggregation is a special form of association where one class is a part of another class, but the part can exist independently of the whole.

    • Aggregation in Java - Scientech Easy

      Apr 23, 2025 · Aggregation in Java is one of the core concepts of object-oriented programming. It focuses on establishing Has-A relationship between two classes. Aggregation is a more …

    • Aggregation in Java: Real-World Examples and Coding Guide

      Apr 3, 2025 · Learn aggregation in Java with real-world examples, beginner-friendly explanations, and clean code samples. Master this essential OOP concept for better software design.

      • Codecademy
        https://www.codecademy.com › get-started › free
        About our ads

        Online Java Courses - Learn by Doing - Coding for All Levels

        SponsoredTake your skills to a new level and join millions of users that have learned Java. Learn key takeaway skills of Java and earn a certificate of completion.
        Courses: Data Science, Computer Science, Web Development, Code Foundations