Aggregation


It is a special form of association where In the following example, there is an Institute which has a number of departments like CS and EE. Every department has a number of students. So, we make an Institute class which has a reference to object or a number of objects (i.e., list of objects) of the Department class. That means Institute class is associated with Department class through its object(s).

And Department class has also a reference to object or objects (i.e., list of objects) of Student class. It means it is associated with Student class through its object(s). It represents a Has-A relationship.

Aggregation.java (an aggregation example)
// Java program to illustrate the concept of aggregation
import java.util.*;

// Main method
class Aggregation {
  public static void main( String[ ] args ) {

    String[ ] names = new String[4];
    for ( int i = 0;  i < 4;  i++ )
      if ( i < args.length )  names[i] = args[i];
      else  names[i] = "";

    Student s1 = new Student( names[0], 1, "CS" );
    Student s2 = new Student( names[1], 2, "CS" );
    Student s3 = new Student( names[2], 1, "EE" );
    Student s4 = new Student( names[3], 2, "EE" );

    // Making a list of CS students
    List<Student> cs_students = new ArrayList<Student>( );
    cs_students.add( s1 );
    cs_students.add( s2 );

    // Making a list of EE students
    List<Student> ee_students = new ArrayList<Student>( );
    ee_students.add( s3 );
    ee_students.add( s4 );

    Department CS = new Department( "CS", cs_students );
    Department EE = new Department( "EE", ee_students );

    List<Department> departments = new ArrayList<Department>( );
    departments.add( CS );
    departments.add( EE );

    // Creating an instance of Institute
    Institute institute = new Institute( "SEECS", departments );

    // System.out.print( "Total students in institute: " );
    System.out.print( institute.getTotalStudentsInInstitute( ) );
  }
}
Student.java (the Student class)
// Class Student
class Student {
  String name;
  int    id;
  String dept;

  Student( String name, int id, String dept ) {
    this.name = name;
    this.id   = id;
    this.dept = dept;
  }
  String getName( ) { return this.name; }
}
Department.java (the Department class)
// Department class contains a list of student objects.
// It is associated with student class through its object(s).
import java.util.*;

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

  Department( String name, List<Student> students ) {
    this.name     = name;
    this.students = students;
  }
  public List<Student> getStudents( ) {
    return students;
  }
}
Institute.java (the Institute class)
// Institute class contains a list of Department objects.
// It is associated with Department class through its object(s).
import java.util.*;

class Institute {
  String instituteName;
  private List<Department> departments;

  Institute( String instituteName, List<Department> departments ) {
    this.instituteName = instituteName;
    this.departments   = departments;
  }
  // Count total students of all departments in a given institute.
  public int getTotalStudentsInInstitute( ) {
    int noOfStudents = 0;
    List<Student> students;
    for ( Department dept : departments ) {
      students = dept.getStudents( );
      for ( Student s : students )
        if ( s.getName( ) != "" )  noOfStudents++;
    }
    return noOfStudents;
  }
}
shell> java Aggregation        

Output:           Result:




      Variety is the spice of life.