Java

What is Aggregation In Java?

A unique kind of relationship is aggregation. It is a connection between two classes, much like an association, except it is directional, meaning it only has one direction. It stands for a HAS-A connection.

Aggregation Example in Java

Consider, for instance, two classes. Address class and the student class. Every student has a home address, therefore there is a Has-A link between the two. However, if you think about it the other way around, it would be absurd since an Address doesn’t always need to have a Student. Let’s create a Java application using this example.

Student Has-A Address

class Address
{
   int streetNum;
   String city;
   String state;
   String country;
   Address(int street, String c, String st, String coun)
   {
       this.streetNum=street;
       this.city =c;
       this.state = st;
       this.country = coun;
   }
}
class StudentClass
{
   int rollNum;
   String studentName;
   //Creating HAS-A relationship with Address class
   Address studentAddr; 
   StudentClass(int roll, String name, Address addr){
       this.rollNum=roll;
       this.studentName=name;
       this.studentAddr = addr;
   }
   public static void main(String args[]){
       Address ad = new Address(55, "Agra", "UP", "India");
       StudentClass obj = new StudentClass(123, "Chaitanya", ad);
       System.out.println(obj.rollNum);
       System.out.println(obj.studentName);
       System.out.println(obj.studentAddr.streetNum);
       System.out.println(obj.studentAddr.city);
       System.out.println(obj.studentAddr.state);
       System.out.println(obj.studentAddr.country);
   }
}

Output:

123
Chaitanya
55
Agra
UP
India

The Aggregation between the Student and Address classes is shown in the sample above. As you can see, I’ve specified a property of type Address in the Student class so that you may get the student’s address. It is a standard Java example of an aggregate.

Why we need Aggregation?

to continue using reused code. Let’s use the same example once again to help you grasp this. Assume that in addition to the two classes listed above, Student and Address, there are two more classes: College and Staff. We don’t have to keep using the same code to retain student addresses, college addresses, and staff addresses. When defining any of these classes, we only need to reference the Address class as follows:

Student Has-A Address (Has-a relationship between student and address)
College Has-A Address (Has-a relationship between college and address)
Staff Has-A Address (Has-a relationship between staff and address)

Therefore, by leveraging aggregation relationships, we may increase code reuse.

Therefore, if I had to program this, I would code it like this:

class Address
{
   int streetNum;
   String city;
   String state;
   String country;
   Address(int street, String c, String st, String coun)
   {
       this.streetNum=street;
       this.city =c;
       this.state = st;
       this.country = coun;
   }
}
class StudentClass
{
   int rollNum;
   String studentName;
   //Creating HAS-A relationship with Address class
   Address studentAddr; 
   StudentClass(int roll, String name, Address addr){
       this.rollNum=roll;
       this.studentName=name;
       this.studentAddr = addr;
   }
   ...
}
class College
{
   String collegeName;
   //Creating HAS-A relationship with Address class
   Address collegeAddr; 
   College(String name, Address addr){
       this.collegeName = name;
       this.collegeAddr = addr;
   }
   ...
}
class Staff
{
   String employeeName;
   //Creating HAS-A relationship with Address class
   Address employeeAddr; 
   Staff(String name, Address addr){
       this.employeeName = name;
       this.employeeAddr = addr;
   }
   ...
}

As you can see, we did not include the Address code in any of the three classes; instead, we merely established a HAS-A connection with the Address class in order to utilize the Address code. The public static void main function may be used in lieu of the dot dot(…) portion of the code above; the code within would be identical to that in the first example.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button