Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Example of Aggregation
2.1.
Code implementation
2.2.
Explanation
3.
When to use Aggregation?
4.
When To Use Aggregation And Inheritance
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Aggregation in Java

Author ANKIT KUMAR
0 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

In Java, we generally have two members in our classes. They are:

  • Fields: used to store data.
    • Example: In a student class, fields can be student_id, student_name, etc.
  • Methods: It is used to perform operations.
    • Example: In a student class, methods can be getGrades(), getMarks(), etc.

It is interesting to note that the fields need not always have to be primitive data types. They can also be objects of another class. Generally, if you have an object as a field in your class, you have an aggregation relationship. There is a kind of HAS-A (read as has-a) relationship between the objects.

Formally, if a class has an entity reference, then it is known as aggregation.

Recommended topic about-  Iteration Statements in Java, Duck Number in Java.

Example of Aggregation

Consider a Course class with fields:

  1. String courseName: String type that holds the name of the course.
  2. int courseID: int type. It represents the unique ID of the specific course.
  3. Teacher teacher: It is an object or reference of Teacher class.

Consider another class named Teacher with fields:

  1. String name: String type. It represents the name of the teacher.
  2. String qualification: String type. It represents the highest qualification of the teacher.
  3. 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

You can also find the output of this java compiler code here.

Explanation

  • 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.

Also see,  Swap Function in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

When to use Aggregation?

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.

    You can also read about the Multiple Inheritance in Java and, Has-A Relationship in Java

 

When To Use Aggregation And Inheritance

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.

Check out this article - Upcasting and Downcasting in Java and Hashcode Method in Java

FAQs

1.What is aggregation in Java?

Ans: 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.

2. Why do we need aggregation?

Ans: The primary reason for aggregation is code reusability. Instead of repeating a particular code, again and again, we create a separate class for it.

3. Which relationship does aggregation follow?

Ans:  It follows the "HAS-A" relationship.

Example: every course has a teacher associated with it.

4. When should we use inheritance, and when should we use aggregation?

Ans: 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.

Key Takeaways

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. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Four Pillars of OOPS in Java
Next article
Inheritance In Java
Live masterclass