Aggregation in Java is one of the core concepts of object-oriented programming. It focuses on establishing Has-A relationship between two classes.
Aggregation is a more specialized form of unidirectional association that represents an ownership relationship between two class objects.
In other words, when two aggregated objects have their own life cycle (i.e. independent lifetime) but one of the objects is the owner of Has-A relationship, it is called aggregation in java.
The owner object is called aggregating object and its class is called aggregating class. The aggregating class has a reference to another class and is the owner of that class.
Having their own relationship means that destroying one object will not affect another object. For example, a library has students. If the library is destroyed, students will exist without library.
Realtime Example of Aggregation Relationship in Java
A most common example of aggregating relationship is “A student has an address”. A student has many pieces of information such as name, roll no, email id, etc.
It also contains one more important object named “address” that contains information such as city, state, country, zip code.
Let’s implement this common example programmatically to understand the aggregation relationship in Java.
In this example program, Student class has an object of Address class where the address object contains its own information such as city, state, country, etc. This relationship is Student Has-A address and is called aggregation.
Look at the program code to understand better.
Program code 1:
public class Address { String city, state, country; int pinCode; public Address(String city, String state, String country, int pinCode) { this.city = city; this.state = state; this.country = country; this.pinCode = pinCode; } } // A student Has-A an address. // Therefore, the Student class must be able to receive address information as follows: public class Student { String name; int rollNo; Address address; int pinCode; public Student(String name, int rollNo, Address address) { this.rollNo = rollNo; this.name = name; this.address=address; } void display() { System.out.println("Name: " +name + ", "+"Roll no: " +rollNo); System.out.println("Address:"); System.out.println(address.city+" "+address.state+" "+address.country+ " " +address.pinCode); System.out.println("\n"); } public static void main(String[] args) { Address addr1 = new Address("Dhanbad,","Jharkhand,","India,", 826001); Address addr2 = new Address("Ranchi,","Jharkhand,","India,", 825001); Student st1 = new Student("Deep", 05, addr1); Student st2 = new Student("John", 02, addr2); st1.display(); st2.display(); } }
Output: Name: Deep, Roll no: 5 Address: Dhanbad, Jharkhand, India, 826001 Name: John, Roll no: 2 Address: Ranchi, Jharkhand, India, 825001
The complete application program is named Aggregation. As you can notice in the above program, a class Student contains a reference to class Address whose instance exists and are accessible outside of Student, we can say that Student is an aggregation of Address.
If Student is an aggregation of Address, we can say that a Student object “Has-A” Address object.
Moving on, let’s take one more example program for practice based on it.
A football player has a football. It is a unidirectional relationship because a football can not have a football player. Even if football player dies, football will not be affected. So, let’s write code for it.
Program code 2:
public class Football { private String type, size, weight; Football(String type, String size, String weight) { this.type = type; this.size = size; this.weight = weight; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getSize() { return size; } public void setSize(String size) { this.size = size; } public String getWeight() { return weight; } public void setWeight(String weight) { this.weight = weight; } } public class FootballPlayer { private String name; private Football football; FootballPlayer(String name, Football football) { this.name = name; this.football = football; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Football getFootball() { return football; } public void setFootball(Football football) { this.football = football; } public static void main(String[] args) { Football football = new Football("Association Football", "68-70 cm", "420 gm" ); FootballPlayer fbp = new FootballPlayer("John", football); System.out.println("Player " +fbp.getName() + " plays with " +fbp.getFootball().getType()); } }
Output: Player John plays with Association Football
When to Use Aggregation in Java?
We use aggregation for code reusability if there is no Is-A relationship. Let’s see a simple example program based on it.
Program code 3:
public class Calculation { int calArea(int length, int breadth) { return (length * breadth); } } public class Rectangle { Calculation cal; // Use of Aggregation. int area(int length, int breadth) { cal = new Calculation(); int areaRec = cal.calArea(length, breadth); // code reusability. return areaRec; } } public class Test { public static void main(String[] args) { Rectangle rec = new Rectangle(); int result = rec.area(25, 60); System.out.println("Area of rectangle: " +result); } }
Output: Area of rectangle: 1500
Difference between Association vs Aggregation
Both association and aggregation are the core concepts of OOP and represent Has-A relationship in Java. But there are also certain important differences between association and aggregation that are as follows:
1. Association establishes the relationship between two classes that are independent of each other whereas, aggregation establishes an ownership relationship between two classes.
2. Association has no owner whereas, aggregation has ownership of association.
3. Association can have relationship such as unidirectional/bidirectional, one-to-one, one-to-many, many-to-one, and many-to-many whereas, aggregation has a unidirectional relationship.
In this tutorial, we have explained almost all the important points related to aggregation in Java with the help of real-time example and programs. Hope that you will have understood the basic concept of aggregation and practiced all programs. In the next, we will understand composition in Java with realtime examples.
Thanks for reading!!!