«Back to Home

Core Java

Topics

Aggregation In Java

Aggregation
 
In Java, aggregation represents “has-a” relationship between two classes and it is a special form of association. If a class has an entity reference, it is called as an aggregation. This helps to reduce duplication of the code and errors.
 
Aggregation is mainly used for the code reusability.
 
Let’s see an example, given below.
 
Code
  1. public class Address {  
  2.     String houseno, city, state, country;  
  3.     public Address(String houseno, String city, String state, String country) {  
  4.         this.houseno = houseno;  
  5.         this.city = city;  
  6.         this.state = state;  
  7.         this.country = country;  
  8.     }  
  9. }  
  1. public class Student {  
  2.     int rollno;  
  3.     String name;  
  4.     Address address;  
  5.     public Student(int r, String n, Address a) {  
  6.         rollno = r;  
  7.         name = n;  
  8.         address = a;  
  9.     }  
  10.     void displayInfo() {  
  11.         System.out.println(rollno + " " + name);  
  12.         System.out.println(address.houseno + " " + address.city + " " + address.state + " " + address.country);  
  13.     }  
  14.     public static void main(String[] args) {  
  15.         Address address1 = new Address("H12""Shahdara""Delhi""India");  
  16.         Address address2 = new Address("H67""Meerut""UP""India");  
  17.         Address address3 = new Address("H90""Surajmal vihar""Delhi""India");  
  18.   
  19.         Student s1 = new Student(112"Deepika", address1);  
  20.         Student s2 = new Student(136"Riya", address2);  
  21.         Student s3 = new Student(116"Rohit", address3);  
  22.         s1.displayInfo();  
  23.         s2.displayInfo();  
  24.         s3.displayInfo();  
  25.     }  
  26. }  
8

9

Output

10
 
In the example, mentioned above, we create two classes Address class and Student class. Suppose, there are many students in college and each student must have a different address. Hence, the relationship between student and address is “Has-A” relationship that shows the aggregation between them.
 
Aggregation main advantage is code reusability and with the usage of aggregation, we can modify the complex code to a simple form and we can easily detect the errors in aggregation.
 
Why we use Aggregation in Java?

In Java, aggregation is used to maintain code in simple form. It is required in order to make and code reusable. We don’t need to write same code again and again, which is already explained in the example, mentioned above. In order to maintain Student’s address, we just have to use the reference of Address class as defining each of these classes. Hence, we can improve the code reusability, using aggregation relationship. Aggregation is a best choice, when there is no IS-A relationship.
 
Summary

Thus, we learnt that aggregation represents “has-a” relationship between two classes. It is a special form of association and we also learnt its usage in Java.