Aggregation in Java
An aggregation is a special form of association. Aggregation is used when we need to use property and behavior of a class without modifying it in the class. Aggregation is also used to achieve code reusability.
- We have two types of relationships one is HAS A and other is IS A relationship and Aggregation represents IS A relationship.
- It is a special form of association i.e. unidirectional association. To understand it, let's take an example: There is an officer and a no. of employees are working for him. Then we can say Officer ‘HAS A ‘employee working for him but vice versa is not true.
- In aggregation both the entity survive individually. If we end one entity that doesn't affect the other entity.
import java.util.*; // employee class class Employee { String name; int id ; String oname;// officer name under whom he work Employee(String name, int id, String oname) { this.name = name; this.id = id; this.oname = oname; } } /* Officer Class contains list of employee Objects. It is associated with Employee Class through its Object(s). */ class Officer { String name; private List< Employee> employees; Officer (String name, List<Employee> employees) { this.name = name; this.employees = employees; } public List<Employee> getemployees() { return employees; } } /* Company class contains list of Employee Objects. It is associated with Officer Class through its Object(s).*/ class Company { String cName; private List<Officer> officers; Company (String cName, List<Officer> officer) { this.cName = cName; this.officers = officer; } // count total employees of all officers // in a given company public int getTotalEmployeesInCompany() { int noOfEmployees = 0; List<Employee> employees; for(Officer oname : officers) { employees = oname.getemployees(); for(Employee e : employees) { noOfEmployees++; } } return noOfEmployees; } } // main method class Main { public static void main (String[] args) { Employee e1 = new Employee("Mia", 1, "Sachin sir "); Employee e2 = new Employee(" Mira", 2, "Sachin sir "); Employee e3 = new Employee("Mahesh ", 3, "Ashish sir "); Employee e4 = new Employee("Manish ", 4, " Ashish sir "); // making a List of // Sachin sir Employees. List <Employee> Sachinsir_employee = new ArrayList<Employee>(); Sachinsir_employee.add (e1); Sachinsir_employee.add (e2); // making a List of // AshishsirEmployees List <Employee> Ashishsir_employee = new ArrayList<Employee>(); Ashishsir_employee.add (e3); Ashishsir_employee.add (e4); Officer o1= new Officer ("Sachinsir", Sachinsir_employee); Officer o2 = new Officer ("Ashishsir", Ashishsir_employee); List <Officer > officers = new ArrayList<Officer>(); officers.add(o2); officers.add(o1); // creating an instance of Company. Company company= new Company("ABC", officers); System.out.println("Total employees in company: "); System.out.println(company.getTotalEmployeesInCompany()); } }Output:
Total employees in company: 4
Example explanation:
In this example, there is a company let's say ABC which has no. of officers like Sachin sir, Ashish sir. Every officer has no. of employees working for him. So, we make a company class which has a reference to Object or no. of Objects (i.e. List of Objects) of the Officer class. That means Company class is associated with Officer class through its Object(s). And Officer class has also a reference to Object or Objects (i.e. List of Objects) of Employee class means it is associated with Employee class through its Object(s).Let's see diagrammatically what happened in this example:
