Introduction To Aggregation In Java

Introduction

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

Aggregation

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 a 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"):

public class SubClass
 
{
    private String sname;
    public void setName(String sname)
      {
        this.sname = sname;
      }
    public String getName()
      {
        return sname;
      }
  }
public class StudentClass
  {
    private SubClass[] studyAreas = new SubClass[10];
    //the rest of the Student class
  }

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:

class AreaEx

  {

    int area(int x, int y)

      {

        return x*y;

      }

  }

class EllipseEx
  {
    AreaEx op;//aggregation
    double pi=3.14;
    double area(int width, int height)
      {
        op=new AreaEx();
        int rsquare=op.area(width, height);//code reusability (i.e delegates the method call).
        return pi*rsquare;
      }
    public static void main(String args[])
      {
        EllipseEx c=new EllipseEx();
        double result=c.area(5, 10);
        System.out.println(result);
      }
  }

 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

public class StAddress
 
{
    String scity,sstate,scountry;
    public StAddress(String scity, String sstate, String scountry)
 
     {
        super();
        this.scity = scity;
        this.sstate = sstate;
        this.scountry = scountry;
      }
  }

 Emp.java

public class Emp

  {

    int sid;

    String sname;

    StAddress add;

    public Emp(int sid, String sname,StAddress add)

      {

        this.sid = sid;

        this.sname = sname;

        this.add=add;

      }

    void print()

      {

        System.out.println(sid+" "+sname);

        System.out.println(add.scity +" "+ add.sstate +" "+ add.scountry);

      }

    public static void main(String args[])

      {

        StAddress add1=new StAddress("San-Fransco","U.S","USA");

        StAddress add2=new StAddress("gno","East","England");

        Emp em1=new Emp(1,"John",add1);

        Emp em2=new Emp(2,"Paul",add2);

        em1.print();

        em2.print();

      }

  }

Output

fig-2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all