Aggregation In Java

In Java, aggregation signifies a HAS-A association where a class includes a reference to another class. This section will delve into the concept of aggregation, providing a definition along with practical instances.

What Is Aggregation in Java?

When a class contains a reference to another class, known as an entity reference, it is referred to as Aggregation. Aggregation signifies a relationship of HAS-A, in which one object utilizes another object as a component of its operations.

Understanding Aggregation with an Example

Imagine a scenario where an Employee object encompasses various details like id, name, and emailId. This object also includes another object called address, which holds details such as city, state, country, and zipcode as illustrated below.

Example

class Employee{

int id;

String name;

Address address;//Address is a class

...

}

In this scenario, an Employee is associated with an entity reference called address, establishing a relationship where the Employee HAS-A address.

Example of Aggregation

In this instance, the Circle class employs the Operation class by utilizing an object reference to compute the area.

Example

Example

class Operation{  

 int square(int n){  

  return n*n;  

 }  

}  



class Circle{  

 Operation op;//aggregation (HAS-A Relationship) 

 double pi=3.14;  

    

 double area(int radius){  

   op=new Operation();  

   int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).  

   return pi*rsquare;  

 }  

}

public class Main{

 public static void main(String args[]){  

   Circle c=new Circle();  

   double result=c.area(5);  

   System.out.println(result);  

 }  

}
Example

Output:78.5

Why use Aggregation?

Here are the advantages of utilizing aggregation in Java:

  • Enhanced Code Reusability: Aggregation facilitates the reuse of code by enabling a class to utilize the functionality of another class without the need for inheritance.
  • Improved Design: Aggregation is beneficial when classes are interconnected through a HAS-A relationship rather than an IS-A relationship.
  • Real-Time Example of Aggregation

In this instance, consider an Employee object that possesses an Address object. The Address object comprises details like city, state, and country. This establishes a relationship where the Employee HAS-A Address.

Example

Example

class Address {  

 String city,state,country;  

  

 public Address(String city, String state, String country) {  

    this.city = city;  

    this.state = state;  

    this.country = country;  

 }  

}  

//Employee class that has Address

class Emp {  

    int id;  

    String name;  

    Address address;  //Emp Has-A Address

  

 public Emp(int id, String name,Address address) {  

    this.id = id;  

    this.name = name;  

    this.address=address;  

 }  

  

 void display(){  

  System.out.println(id+" "+name);  

  System.out.println(address.city+" "+address.state+" "+address.country);  

 }  

}

//Main class 

public class Main{

 public static void main(String[] args) {  

  Address address1=new Address("gzb","UP","india");  

  Address address2=new Address("gno","UP","india");  

  Emp e=new Emp(111,"varun",address1);  

  Emp e2=new Emp(112,"arun",address2);  

      

  e.display();  

  e2.display();    

 }  

}
Example

111 varun

       gzb UP india

       112 arun

       gno UP india

Input Required

This code uses input(). Please provide values below: