Java Aggregation

    Java Aggregation

    Aggregation refers to a class having the entity reference. It shows a HAS-A relationship. Considering a scenario, if we have an employee object, it will contain information like id, name, and address. But in turn, the address will contain information like zip code, city, state, area, and so on.

    Hence, the address will be an entity reference for the employee, which represents the Employee HAS-A address relationship. Like inheritance, it allows you to reuse the existing code.


    class Employee{  
       int id;  
       String name;  
       Address address;

    Aggregation example

    class Operation{  
     int square(int n){  
      return n*n;  
    class Circle{  
     Operation op;//aggregation  
     double pi=3.14;  
     double area(int radius){  
       op=new Operation();  
       int r square=op.square(radius);
       return pi*rsquare;  
     public static void main(String args[]){  
       Circle c=new Circle();  
       double result=c.area(5);  



    Aggregation is commonly used, if there is no is-a relationship in the program then aggregation is the alternative option to ensure the code reusability.