About Lesson
Introduction:
Aggregation is another form of association in Java. But in the case of aggregation, it only allows the one-way relationship between the objects.
For example, an employee has a home address. But vice versa, the home address has employees, doesn’t sound right. Similarly, a student has an address but “address has a student” doesn’t make sense. Like composition, aggregation also depicts the “has-a” relationship. This means a class contains an object of another class.
The below diagram represents an Aggregation example.
The above example can be interpreted as the College has staff and students.
So when exactly should we go for Aggregation?
We should use aggregation when there is no need for an “is-a” relationship or inheritance. If we can maintain the ‘is-a’ relationship throughout the application or lifetime of an object, then we can implement inheritance for code reuse. Otherwise, it is best to use aggregation for code reusability. Let’s now implement an example aggregation in Java.
The example system we have used is as follows:
Here we have an ‘Institute’ class. An institute can have various departments or branches. Each branch in turn has several students. In this program, we will count the total number of students in the entire institute. We use aggregation for this purpose. The class Institute contains the Branch object. The branch object has a student object. So in the Institute class, we count the total number of students using the Branch object. For this purpose, we use a list of branches in an institute.
The Java program is given below
import java.io.*;
import java.util.*;
// Class Student
class Student {
String student_name;
int student_id;
String student_dept;
// Initialize Student class members
Student(String student_name, int student_id, String student_dept) {
this.student_name = student_name;
this.student_id = student_id;
this.student_dept = student_dept;
}
}
// Branch class indicates the branch or department to which the student belongs
class Branch {
String Branch_name;
private List<Student> students; // Each branch contains students
// Initialize class members
Branch(String Branch_name, List<Student> students) {
this.Branch_name = Branch_name;
this.students = students;
}
// Return list of students
public List<Student> getStudents() {
return students;
}
}
// Institute class contains branches which in turn have students
class Institute {
String instituteName;
private List<Branch> branches; // Each institute has various branches
// Initialize members
Institute(String instituteName, List<Branch> branches) {
this.instituteName = instituteName;
this.branches = branches;
}
// Count and return number of all students in the institute
public int getAllStudentsInInstitute() {
int noOfStudents = 0;
List<Student> students;
for (Branch branch : branches) {
students = branch.getStudents();
for (Student s : students) {
noOfStudents++;
}
}
return noOfStudents;
}
}
// Aggregate all the classes => Institute (contains) branches (contain) students
class Main {
public static void main(String[] args) {
// Declare student objects
Student s1 = new Student("Megan", 1, "CSE");
Student s2 = new Student("Mia", 2, "CSE");
Student s3 = new Student("John", 1, "ETC");
Student s4 = new Student("Finn", 2, "ETC");
// List of CSE Students
List<Student> cse_students = new ArrayList<Student>();
cse_students.add(s1);
cse_students.add(s2);
// List of ETC Students
List<Student> etc_students = new ArrayList<Student>();
etc_students.add(s3);
etc_students.add(s4);
// Declare Branch objects
Branch CSE = new Branch("CSE", cse_students);
Branch ETC = new Branch("ETC", etc_students);
// Make list of branches
List<Branch> branches = new ArrayList<Branch>();
branches.add(CSE);
branches.add(ETC);
// Creating an object of Institute
Institute institute = new Institute("NIT", branches);
// Display total number of students
System.out.print("Total students in NIT institute: ");
System.out.print(institute.getAllStudentsInInstitute());
}
}