Aggregation in Java

Aggregation can be said as a relation between two classes that is best described as a has-a and whole/part relationship. It is a more specialized version of the association relationship. Now, what is association? In Java, when you think of a class having that has a member of the different type, then there an association can be formed. In a Java class, where there lies an entity reference, it becomes aggression. Let us take a situation; Student object contains much information such as roll, name, email_add, etc. It contains one more object named rank, which contains its information such as subj_Name, marks, board_name, exam-type, etc. as given below.

class Student { int roll; String name; Rank ranking; }

In this case, Student has an entity reference ranking, so the relationship is Student HAS-A ranking Aggregation is also used for code reusability. An aggregation is a form of association where the relation of Association can be considered the containing class 'owning' the contained class. The lifetime of that relationship cannot be defined. 'Owning' can be determined as a single-direction Association.

Why programmers use Aggression in Java?

As told earlier, for code reusability. Let us consider a case where we have used two classes, student class, and another id class along with staffs and teacher; to maintain Student id, teachers' id, and staff id, programmers do not need to use the same code again and again. Just use the reference of id class while defining each of these classes. This can be written as:

Student Has-A id (Has-a relationship between student and id) Staff Has-A id (Has-a relationship between staff and id) teachers Has-A id (Has-a relationship between teachers and id)
For Code Reusability.
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 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(7); System.out.println(result); } }


Output:
153.86
import java.util.Scanner; 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[]) { int radious; Scanner p=new Scanner(System.in); System.out.println("Enter radious of circle:"); radious=p.nextInt(); Circle c=new Circle(); double result=c.area(radious); System.out.println(result); } }


Output:
Enter radious of circle: 10 314.0
When use Aggregation?
  • Code reuse is also best achieved by aggregation when there is no is-a relationship.

  • Inheritance should be used only if the relationship is-a is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.

Understanding meaningful example of Aggregation

In this example, Employee has an object of Address, address object contains its own informations such as city, state, country etc. In such case relationship is Employee HAS-A address.

Address.java
public class Address { String city,state,country; public Address(String city, String state, String country) { this.city = city; this.state = state; this.country = country; } }


Emp.java
public class Emp { int id; String name; Address 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); } public static void main(String[] args) { Address address1=new Address("HYD","UP","india"); Address address2=new Address("HYD","UP","india"); Emp e=new Emp(31,"Pavi",address1); Emp e2=new Emp(36,"Rama",address2); e.display(); e2.display(); } }


Output:
31 Pavi HYD UP india 112 Rama HYD UP india



Instagram