Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Aggregation in Java and why do you need it?

Last updated on Nov 27,2019 5.7K Views

55 / 72 Blog from Java Core Concepts

When you are writing a Java program, if you want to link one class to another using its reference, you can make use of Aggregation in Java. So, let’s learn how Aggregation works in Java.

What is Aggregation?

Before you understand what is Aggregation, let’s learn about Association in Java. Association is referred as a relation between two separate classes which establishes through their Objects. It can be one-to-one, one-to-many, many-to-one, many-to-many. Let’s understand about Association with an example. 

package Edureka;
class School
{
private static String name;

// bank name
School(String name)
{
this.name = name;
}

public static String getSchoolName()
{
return name;
}
}

// employee class
class Student
{
private String name;

// employee name
Student(String name)
{
this.name = name;
}

public String getStudentName()
{
return this.name;
}
}

// Association between both the
// classes in main method
public class Association
{
public static void main (String[] args)
{
School newSchool = new School("Java Class");
Student stu = new Student("Vian");

System.out.println(stu.getStudentName() +
" is a student of " + School.getSchoolName());
}
}

Output: Vian is a student of Java Class

Now, let’s see what is Aggregation in Java.

Aggregation is actually a special form of association. This means that it is referred as the relationship between two classes like Association. However, it’s a directional association, which means it strictly follows a one-way association. This represents a HAS-A relationship.

It is considered as a more specialized version of the Association relationship. The Aggregate class contains a reference to another class and is said to have the ownership of that class. Each class that is referenced is considered to be a part of the Aggregate class.

Now say, for example, if Class A contains a reference to Class B and Class B contains a reference to Class A then no clear ownership can be determined and the relationship is simply one of Association.

Let’s take a look at this example:


package Edureka;

class Address
{
int streetNum;
String city;
String state;
String country;
Address(int street, String c, String st, String coun)
{
this.streetNum=street;
this.city =c;
this.state = st;
this.country = coun;
}
}
class Employee
{
int EmployeeID;
String EmployeeName;
//Creating HAS-A relationship with Address class
Address EmployeeAddr;
Employee(int ID, String name, Address addr){
this.EmployeeID=ID;
this.EmployeeName=name;
this.EmployeeAddr = addr;
}
}
public class Aggregation {
public static void main(String args[]){
Address ad = new Address(2, "Bangalore", "Karnataka", "India");
Employee obj = new Employee(1, "Suraj", ad);
System.out.println(obj.EmployeeID);
System.out.println(obj.EmployeeName);
System.out.println(obj.EmployeeAddr.streetNum);
System.out.println(obj.EmployeeAddr.city);
System.out.println(obj.EmployeeAddr.state);
System.out.println(obj.EmployeeAddr.country);
}
}

Output:

Aggregation in Java Output- Edureka

Now you might have this question. Why exactly should you use this Aggregation in Java?

Why do you need Aggregation?

The main reason why you need Aggregation is to maintain code reusability. For example, if you create a class the same as the above example, you need to maintain the details of the Employee present. And, you don’t have to use the same code again and again but instead, use the reference of the class while you define them.

This brings us to the end of this article where we have learned about Aggregation in Java. Hope you are clear with all that is shared with you in this tutorial.

If you found this article on “Aggregation in Java” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey. This curriculum is designed for students and professionals who are willing to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “Aggregation in Java” and our team will be glad to answer.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Aggregation in Java and why do you need it?

edureka.co