Aggregation in Java

If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship.

Consider a situation, Employee object contains many informations such as id, name, emailId etc. It contains one more object named address, which contains its own informations such as city, state, country, zipcode etc. as given below.

class Employee{
int id;
String name;
Address address;//Address is a class

In such case, Employee has an entity reference address, so relationship is Employee HAS-A address.

Why use Aggregation?

  • For Code Reusability.

Simple Example of Aggregation

In this example, we have created the reference of Operation class in the Circle class.

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 rsquare=op.square(radius);//code reusability (i.e. delegates the method call).
   return pi*rsquare;

 public static void main(String args[]){
   Circle c=new Circle();
   double result=c.area(5);
Related Tutorial
Follow Us #