Aggregation in Java
Aggregation
A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation. Another class is referred to as belonging to the aggregate class since it has a reference to it. Each class that is referred to is regarded as being a component of the aggregate class.
Due to the fact that an aggregate connection cannot contain cyclic references, ownership happens. No unambiguous ownership can be shown, and the relationship is merely one of association if Class A and Class B both include references to one other. It symbolises the connection between Has-A.
It is a one-way, or unidirectional, association. For instance, a department may have students, but students can’t have departments, making this relationship unidirectional.
Since both items in Aggregation can persist on their own, eliminating one won't have an impact on the other.
Let’s learn this topic in an easier way with an example code.
//Java application to display the
// The Concept of the Aggregation
// importing the necessary classes
import java.io.*;
import java.util.*;
// Class 10
// learner class
class Learner {
// Qualities of a Learner
String name;
int id;
String cate;
// Constructor of the learner class
Learner(String name, int id, String cate)
{
// The present instance is referred to by this keyword.
this.name = name;
this.id = id;
this.cate = cate;
}
}
// Class 9
// Objects for learners are included in the category class.
// It connects to the learner class due to its Objects.
class Category {
// Qualities of the Category class
String name;
private List<Learner> Learners;
Category(String name, List<Learner> Learners)
{
// this term designates the actual instance being used.
this.name = name;
this.Learners = Learners;
}
// The category class method
public List<Learner> getLearners()
{
// giving back a list of user-defined types
// type of learner
return Learners;
}
}
// Class 8
// List of Categories in a University Class
// Objects. It is associated with the Category
// class employing its Objects
class university {
// Qualities of the university
String universityName;
private List<Category> Categories;
// Constructor of the university class
university(String universityName,List<Category> Categories)
{
// The present instance is referred to by this keyword.
this.universityName = universityName;
this.Categories = Categories;
}
// Method of the university class
// Number of Learners in Total across All Categories
// in a given university
public int getTotalLearnersInuniversity()
{
int noOfLearners = 0;
List<Learner> Learners;
for (Category cate : Categorys) {
Learners = .getcateLearners();
for (Learner s : Learners) {
noOfLearners++;
}
}
return noOfLearners;
}
}
// Class 7
// main class
class Kamal {
// main driver method
public static void main(String[] args)
{
// Creating object of learner class inside main()
Learner h1 = new Learner("Konda", 10, "EEE");
Learner h2 = new Learner("Sahithi", 12, "EEE");
Learner h3 = new Learner("Uppi", 13, "IT");
Learner h4 = new Learner("Hymavathi", 21, "IT");
// Making a List of Learners in EEE
List<Learner> cse_Learners = new ArrayList<Learner>();
// Adding the EEE Learners
cse_Learners.add(h1);
cse_Learners.add(h2);
// making a List of IT Learners
List<Learner> it_Learners
= new ArrayList<Learner>();
// Adding EE Learners
it_Learners.add(h3);
it_Learners.add(h4);
// making objects of IT and EEE class the inside
// main()
Category EEE = new Category("EEE", eee_Learners);
Category IT = new Category("IT", it_Learners);
List<Category> Categories = new ArrayList<Category>();
Categories.add(EEE);
Categories.add(IT);
// Lastly, creating an instance of the university
university university = new university("BVRIT", Categories);
// Display the message for easier reading
System.out.print("Total Learners in the university: ");
// getting the total number of students by calling a method
// in the university and printing on console
System.out.print(university.getTotalLearnersInuniversity());
}
}
Output:

Explanation of the output:
In this illustration, there is a university with numerous categories, including EEE and IT. There are in every category. So, we create a category class object or number of objects (i.e., List of Objects) by referencing it in the university class. It indicates that the category class's Object links the university class to the category class (s). Additionally, the category class refers to the learner class's Objects or Objects (i.e., List of Objects), indicating that it is connected to the learner class via its Object (s). It stands for a Has-A relation. In the aforementioned illustration: the learner Student possesses a name. The learner has an ID. A category. Has A Learner category Has learners.
When should you utilise aggregation?
- In situations with no is-a relationship, aggregation also yields the best results for code reuse.
- If the relationship is maintained throughout the lifespan of the objects involved, inheritance should only be utilised; otherwise, aggregation is the preferable option.