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)
01// Java program to illustrate the concept of aggregation
02import java.util.*;
03 
04// Main method
05class Aggregation {
06  public static void main( String[ ] args ) {
07 
08    String[ ] names = new String[4];
09    for ( int i = 0;  i < 4;  i++ )
10      if ( i < args.length )  names[i] = args[i];
11      else  names[i] = "";
12 
13    Student s1 = new Student( names[0], 1, "CS" );
14    Student s2 = new Student( names[1], 2, "CS" );
15    Student s3 = new Student( names[2], 1, "EE" );
16    Student s4 = new Student( names[3], 2, "EE" );
17 
18    // Making a list of CS students
19    List<Student> cs_students = new ArrayList<Student>( );
20    cs_students.add( s1 );
21    cs_students.add( s2 );
22 
23    // Making a list of EE students
24    List<Student> ee_students = new ArrayList<Student>( );
25    ee_students.add( s3 );
26    ee_students.add( s4 );
27 
28    Department CS = new Department( "CS", cs_students );
29    Department EE = new Department( "EE", ee_students );
30 
31    List<Department> departments = new ArrayList<Department>( );
32    departments.add( CS );
33    departments.add( EE );
34 
35    // Creating an instance of Institute
36    Institute institute = new Institute( "SEECS", departments );
37 
38    // System.out.print( "Total students in institute: " );
39    System.out.print( institute.getTotalStudentsInInstitute( ) );
40  }
41}
Student.java (the Student class)
01// Class Student
02class Student {
03  String name;
04  int    id;
05  String dept;
06 
07  Student( String name, int id, String dept ) {
08    this.name = name;
09    this.id   = id;
10    this.dept = dept;
11  }
12  String getName( ) { return this.name; }
13}
Department.java (the Department class)
01// Department class contains a list of student objects.
02// It is associated with student class through its object(s).
03import java.util.*;
04 
05class Department {
06  String name;
07  private List<Student> students;
08 
09  Department( String name, List<Student> students ) {
10    this.name     = name;
11    this.students = students;
12  }
13  public List<Student> getStudents( ) {
14    return students;
15  }
16}
Institute.java (the Institute class)
01// Institute class contains a list of Department objects.
02// It is associated with Department class through its object(s).
03import java.util.*;
04 
05class Institute {
06  String instituteName;
07  private List<Department> departments;
08 
09  Institute( String instituteName, List<Department> departments ) {
10    this.instituteName = instituteName;
11    this.departments   = departments;
12  }
13  // Count total students of all departments in a given institute.
14  public int getTotalStudentsInInstitute( ) {
15    int noOfStudents = 0;
16    List<Student> students;
17    for ( Department dept : departments ) {
18      students = dept.getStudents( );
19      for ( Student s : students )
20        if ( s.getName( ) != "" )  noOfStudents++;
21    }
22    return noOfStudents;
23  }
24}
shell> java Aggregation        

Output:           Result:




      Variety is the spice of life.