×

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 Learners;
Category(String name, List Learners)
{
// this term designates the actual instance being used.
this.name = name;
this.Learners = Learners;
}
// The category class method
public List 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 Categories;
// Constructor of the university class
university(String universityName,List 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 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 cse_Learners = new ArrayList();
// Adding the EEE Learners
cse_Learners.add(h1);
cse_Learners.add(h2);
// making a List of IT Learners
List it_Learners
= new ArrayList();
// 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 Categories = new ArrayList();
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:

Aggregation in Java

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?

  1. In situations with no is-a relationship, aggregation also yields the best results for code reuse.
  2. If the relationship is maintained throughout the lifespan of the objects involved, inheritance should only be utilised; otherwise, aggregation is the preferable option.

Related Topics

Java Integer toString() method

The toString() method of Java Integer class returns a String object which represents this Integer’s value. The second syntax returns a String object which represents the specified integer. The third syntax returns...

2 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Race Condition in Java

Java is a multi-threaded programming language, race conditions are more likely to arise. Mostly because data can change when multiple threads visit the same resource simultaneously. Race conditions are concurrency...

3 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

Java String isEmpty() method

Java String isEmpty() method checks whether current String is empty or not. Syntax: public boolean isEmpty() Returns It returns true, if length of String is 0 otherwise false. Java String isEmpty() method example 1    ...

1 minute read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

5 minutes read.

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To...

6 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Java String

Definition A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you...

4 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.