Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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.
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:
Now you might have this question. Why exactly should you use this Aggregation in Java?
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co