Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Aggregation is a fundamental concept in object-oriented programming (OOP) that represents a "has-a" relationship between objects. In Java, it is used to model complex systems where one class is composed of other objects, allowing for more modular and reusable code. Unlike inheritance, where a subclass derives from a superclass, aggregation allows an object to contain instances of other classes without tightly coupling them. This promotes flexibility and better code maintenance. In this blog, we will explore how aggregation works in Java.
Aggregation in Java is a type of association that represents a "has-a" relationship between two classes. It is a design principle where one class contains references to objects of other classes, meaning one object can be part of another object. However, unlike inheritance, aggregation allows the contained objects to exist independently of the container object.
In other words, aggregation indicates that a class can have objects of other classes as part of its state, but those objects can exist outside the context of the containing class as well.
Example of Aggregation
Consider a Course class with fields:
String courseName: String type that holds the name of the course.
int courseID: int type. It represents the unique ID of the specific course.
Teacher teacher: It is an object or reference of Teacher class.
Consider another class named Teacher with fields:
String name: String type. It represents the name of the teacher.
String qualification: String type. It represents the highest qualification of the teacher.
int teacherID: int type. It represents the unique ID of a teacher.
Every course "has a" teacher associated with it. Every time we want to add a new course in our program, we can either write the code in teacher class again and again or we can simply create a separate class named "Teacher" which contains all the information about teachers. In this way, we can associate the course with a teacher.
It is better to separate the Course from the Teacher. Course class contains separate fields which are independent of the fields in Teacher class.
Code implementation
In this section, we will implement both the Course class and the Teacher class in Java. We shall then discuss the concept of aggregation with the help of code.
Teacher.java
public class Teacher{
String name;
String qualification;
int teacherID;
public Teacher(String name, String qualification, int teacherID){
this.name= name;
this.qualification= qualification;
this.teacherID= teacherID;
}
}
Course.java
public class Course{
// fields for the Course class
String courseName;
int courseID;
//aggregation
Teacher teacher; // reference of the Teacher class
//constructor for the Course
public Course(String courseName, int courseID, Teacher teacher){
this.courseName=courseName;
this.courseID= courseID;
this.teacher=teacher;
}
public static void main(String args[]){
// create two Teacher objects
Teacher teacher1= new Teacher("John", "PhD",101);
Teacher teacher2= new Teacher("Howard", "MS",102);
//create Course object
//We pass the teacher objects we created as the third parameter
Course course1= new Course("Math",1234, teacher1);
Course course2= new Course("DSA",3456, teacher2);
System.out.println(course1.courseName+" is taught by Prof. "+course1.teacher.name);
System.out.println(course2.courseName+" is taught by Prof. "+course2.teacher.name);
}
}
Output:
Math is taught by Prof. John
DSA is taught by Prof. Howard
We created a separate Teacher class that contained the relevant information related to teachers.
Instead of writing the code regarding the teachers in the Course class, we rather create a reference of the Teacher class (Teacher teacher) inside the Course class.
Before the creation of the object of the Course class, the object of the Teacher class must be created.
We pass the object reference as the parameter in the Course object.
We can fetch all the information of the Teacher class using the Course object by simply using the dot operator.
Example: course1.teacher.name provides the name of the teacher who takes course 1.
There are certain benefits of aggregation in Java.
The primary reason for aggregation is code reusability. Instead of trying to code the details of the teachers, again and again, we can simply create a separate Teacher class, and whenever the fields or methods of the Teacher class are required to be accessed, we can simply create an object reference of it.
Whenever there is a HAS-A relationship, aggregation is very well suited.
Most people are confused about when to use aggregation and when to use inheritance. Whenever there is a HAS-A relationship, we go for aggregation. Whenever there is an "IS-A" relationship, we go for inheritance.
Example: Cats are a type of animal. In this case, we go for inheritance. The Cat class can extend the Animal class and override the required methods and fields.
Example: Every course has a teacher. In this case, we have a "HAS-A" relationship, so we go for aggregation.
Aggregation is used to model relationships where objects can exist independently, promoting reusability, flexibility, and modularity in object-oriented design without tight coupling.
What is Aggregation and Types?
Aggregation is a "has-a" relationship between classes where one class contains objects of another. There are two types: unidirectional (one-way) and bidirectional (two-way) aggregation.
Why do we need aggregation?
The primary reason for aggregation is code reusability. Instead of repeating a particular code, again and again, we create a separate class for it.
Which relationship does aggregation follow?
It follows the "HAS-A" relationship.
Example: every course has a teacher associated with it.
When should we use inheritance, and when should we use aggregation?
In the case of the "HAS-A" relationship, we go for aggregation, whereas in the case of the "IS-A" relationship, we go for inheritance.
Conclusion
In this article, we have extensively discussed the concept of aggregation in Java and code implementation of the same.
If a class has an entity reference, then it is known as aggregation. It is a term that is used to refer to a one-way relationship between two objects.
The primary reason for aggregation is code reusability. Instead of repeating a particular code, again and again, we create a separate class for it.
We hope that this blog has helped you enhance your knowledge regarding aggregation or Java and if you would like to learn more, check out our articles here.