Introduction To Aggregation In Java

Introduction

 
In this article, we discuss aggregation in Java, including its need and use.
 

What is Aggregation in Java?

 
It is an updated version of the association relationship. This class contains a reference to another class and is said to have ownership of that class. Each referenced class is considered to be a part of the aggregate class. It shows a relationship between two classes, in other words, "has-a" and "whole/part" relationship.
 
For example, consider a Student class (StudentClass) that contains student information for a school. Now let's say there is another class, Subject class (SubClass), that contains the details about a particular subject (e.g., English, math). Now, the purpose of the Student class is to contain Subject objects. Hence, a Student object is, therefore, the owner of the Subject object.
 
Examples: 
 
There is an aggregation relationship between Student class ("StudentClass") and the Subject class ("SubClass"):
  1. public class SubClass {    
  2.  private String sname;    
  3.  public void setName(String sname) {    
  4.   this.sname = sname;    
  5.  }    
  6.  public String getName() {    
  7.   return sname;    
  8.  }    
  9. }    
  10. public class StudentClass {    
  11.  private SubClass[] studyAreas = new SubClass[10];    
  12. }     

Why we need aggregation?

 
The main purpose of aggregation is the reuse of code, in other words, it saves a lot of time by eliminating the need to write the same code many times.
 
Example
 
In this example, we have created the reference of the AreaEx class in the EllipseEx class:
  1. class AreaEx {  
  2.  int area(int x, int y) {  
  3.   return x * y;  
  4.  }  
  5. }  
  6. class EllipseEx {  
  7.  AreaEx op;  
  8.  double pi = 3.14;  
  9.  double area(int width, int height) {  
  10.   op = new AreaEx();  
  11.   int rsquare = op.area(width, height);  
  12.   return pi * rsquare;  
  13.  }  
  14.  public static void main(String args[]) {  
  15.   EllipseEx c = new EllipseEx();  
  16.   double result = c.area(510);  
  17.   System.out.println(result);  
  18.  }  
  19. }  
Output
 
fig-1.jpg
 

Use of aggregation

 
When we use aggregation:
  • When there is no is-a relationship, code can be reused easily using the aggregation.
  • Inheritance is applicable only if there is a relationship between the objects throughout the lifetime, otherwise, aggregation is the best choice.

Another example

 
In this example, Emp has an object of StAddress and the add object contains its own information, such as scity, sstate, scountry etcetera. In such a case the relationship is Emp has-an add.
 
StAddress.java 
  1. public class StAddress {  
  2.  String scity, sstate, scountry;  
  3.  public StAddress(String scity, String sstate, String scountry) {  
  4.   super();  
  5.   this.scity = scity;  
  6.   this.sstate = sstate;  
  7.   this.scountry = scountry;  
  8.  }  
  9. }  
Emp.java
  1. public class Emp {  
  2.  int sid;  
  3.  String sname;  
  4.  StAddress add;  
  5.  public Emp(int sid, String sname, StAddress add) {  
  6.   this.sid = sid;  
  7.   this.sname = sname;  
  8.   this.add = add;  
  9.  }  
  10.  void print() {  
  11.   System.out.println(sid + " " + sname);  
  12.   System.out.println(add.scity + " " + add.sstate + " " + add.scountry);  
  13.  }  
  14.  public static void main(String args[]) {  
  15.   StAddress add1 = new StAddress("San-Fransco""U.S""USA");  
  16.   StAddress add2 = new StAddress("gno""East""England");  
  17.   Emp em1 = new Emp(1"John", add1);  
  18.   Emp em2 = new Emp(2"Paul", add2);  
  19.   em1.print();  
  20.   em2.print();  
  21.  }  
  22. }   
Output
 
fig-2.jpg
 

Summary

 
In this article, we learned about aggregation in Java and how we use Aggregation in our Java Program.