Back to: Java Tutorials For Beginners and Professionals
Association Composition and Aggregation in Java
In this article, I am going to discuss Association Composition and Aggregation in Java with Examples. Please read our previous where we discussed Interface in Java with Examples. At the end of this article, you will understand what is Association Composition and Aggregation in Java and what is the relationship between them in detail.
Association in Java:
If two classes in a model need to communicate with each other, there must be a link between them, and that can be represented by an association (connector). Association means that the objects “know” each other. For example, a mother and her child. It refers to how objects are related to each other and how they are using each other’s functionality.
Association can be represented by a line between these classes with an arrow indicating the navigation direction. In case an arrow is on both sides, the association is known as a bidirectional association. We can indicate the multiplicity of an association by adding multiplicity adornments to the line denoting the association.
Example to Understand Association in Java:
package Demo; import java.io.*; //class College class College { private String name; // College name College (String name) { this.name = name; } public String getCollegeName () { return this.name; } } //Student class class Student { private String name; // student name Student (String name) { this.name = name; } public String getStudentName () { return this.name; } } //Association between both the //classes in main method public class AssociationDemo { public static void main (String[]args) { College college = new College ("Sri Chaitanya"); Student s = new Student ("Anurag"); System.out.println (s.getStudentName () + " is student of " +college.getCollegeName ()); } }
Output: Anurag is Student of Sri Chaitanya
Types of Associations in Java:
Basically, there are two forms of Association:
- Aggregation
- Composition
Aggregation in Java:
Aggregation is also a “has-a” relationship. Aggregation is a weak association. We call aggregation those relationships whose objects have an independent lifecycle, but there is ownership, and child objects cannot belong to another parent object. Aggregation in Java follows a one-way or one-to-one relationship. Ending one entity won’t affect another, both can be present independently.
Let’s take the example of a mobile phone and a battery. A single battery can belong to a mobile phone, but if the mobile phone stops working, we delete it from our database. The phone battery will not be deleted because it may still be functional. So in aggregation, while there is ownership, objects have their own lifecycle.
Note: Code reuse is best achieved by aggregation.
Example to Understand Aggregation in Java:
package Demo; class Employees { String name; int id; String dept; Employees (String name, int id, String dept) { this.name = name; this.id = id; this.dept = dept; System.out.println ("Employee name is " + name + " | Id is " + id +" | Department is " + dept); } } class Department { String name, employees; Department (String name, String employees) { this.name = name; this.employees = employees; } } class Organization { String officeName, departments; Organization (String officeName, String departments) { this.officeName = officeName; this.departments = departments; } } public class AggregationDemo { public static void main (String[]args) { Employees s1 = new Employees ("Mia", 1, "Sales"); Employees s2 = new Employees ("Priya", 2, "Marketing"); Employees s3 = new Employees ("John", 1, "IT"); Employees s4 = new Employees ("Rahul", 2, "Designing"); } }
Output:
Composition in Java:
The composition is a restricted form of Aggregation in which two entities are highly dependent on each other. We use the term composition to refer to relationships whose objects don’t have an independent lifecycle, and if the parent object is deleted, all child objects will also be deleted. Composition in Java represents a one-to-many relationship.
For example, a room belongs to a building, or in other words a building has a room. So basically, whether we call it “belongs to” or “has-a” is only a matter of point of view.
Example to Understand Composition in Java:
package Demo; import java.io.*; import java.util.*; // class book class Book { public String title; public String author; Book (String title, String author) { this.title = title; this.author = author; } } // Library class contains // list of books. class Library { // reference to refer to list of books. private final List < Book > books; Library (List < Book > books) { this.books = books; } public List < Book > getTotalBooksInLibrary () { return books; } } // main method class CompositionDemo { public static void main (String[]args) { // Creating the Objects of Book class. Book b1 = new Book ("EffectiveJ Java", "Joshua Bloch"); Book b2 = new Book ("Thinking in Java", "Bruce Eckel"); Book b3 = new Book ("Java: The Complete Reference", "Herbert Schildt"); // Creating the list which contains the // no. of books. List < Book > books = new ArrayList < Book > (); books.add (b1); books.add (b2); books.add (b3); Library library = new Library (books); List < Book > bks = library.getTotalBooksInLibrary (); for (Book bk:bks) { System.out.println ("Title : " + bk.title + " and " + " Author : " + bk.author); } } }
Output:
In the next article, I am going to discuss Garbage Collection in Java with Examples. Here, in this article, I try to explain Association Composition and Aggregation in Java with Examples. I hope you enjoy this Association Composition and Aggregation in Java article. I would like to have your feedback. Please post your feedback, question, or comments about this Association Composition and Aggregation in Java article.