Compare the Difference Between Similar Terms

Difference Between Aggregation and Composition in Java

Key Difference – Aggregation vs Composition in Java

Aggregation is an association between two objects that describes the “has-a” relationship. The composition is the more specific type of aggregation that implies ownership. The key difference between aggregation and composition in Java is that, if the contained object can exist without the existence of the owning object, it is an aggregation, and if the contained object cannot exist without the existence of the owning object, it is a composition.

Object-Oriented Programming (OOP) is a major paradigm in software development. It is used to model the software using objects. The objects are created using classes. A class consist of properties and methods. There are multiple objects in software. Each object collaborates with each other through message passing. The relationship between two objects is known as an association. Both aggregation and composition are two types of association. The “has-a” relationship describes that one object can use another object. Aggregation and composition can be implemented in OOP supporting languages. If the contained object can exist without the existence of the owning object, then the association between those two objects is an aggregation. If the contained object cannot exist without the existence of the owning object, then the association between those two objects is a composition.

CONTENTS

1. Overview and Key Difference
2. What is Aggregation in Java
3. What is Composition in Java
4. Similarities Between Aggregation and Composition in Java
5. Side by Side Comparison – Aggregation vs Composition in Java in Tabular Form
6. Summary

What is Aggregation in Java?

Aggregation is a type of association. If a class has an entity reference, it is known as aggregation. Aggregation represents the has-a relationship. A Student object can have properties such as student_id, name, address. This object can also have another object called address with its own information such as city, state, country.  In this situation, the Student has an entity reference address. It is a “has-a” relationship.

Figure 01: Marks Class

 

Figure 02: Main Program to describe Aggregation

According to the above program, the class Marks consist of three properties that are Maths, English and Science marks. The Student has an object of Marks. It has its own properties that are marks of maths, English and science. In the main method, an object of Marks is created and marks values are assigned. The student object that is s1 can use the marks object which is m1. Therefore, the Student and Marks have the “has-a” relationship. The Marks object can exist without the Student Object. Therefore, it is an aggregation.

What is Composition in Java?

Composition is a type of association. It is a specific form of aggregation that implies ownership. Assume that there are two classes called class A and B. If the object of class B cannot exist if the object of class A is destroyed, then that is a composition. A book consists of many pages. If the book is destroyed, the pages will also destroy. The page objects cannot exist without the book object. Refer the below program.

Figure 03: Classroom Class

 

Figure 04: School Class

 

Figure 05: Main Program to describe Composition

According to the above program, the Classroom has two properties that are name and numOfStudents. The School is a collection of Classroom objects. In the main method, two Classroom objects are created. Those are added to ‘classrooms’. These ‘classrooms’ are passed to the school object. Finally, the classroom name and num of students are printed by iterating through the collection. If the School object is destroyed, the Classroom objects will also destroy. This is an example of composition. It also contains the ‘has-a’ relationship and also implies ownership.

What is the Relationship Between Aggregation and Composition in Java?

What is the Difference Between Aggregation and Composition in Java?

Aggregation vs Composition in Java

Aggregation is an association between two objects that describes the “has a” relationship. Composition is a more specific type of aggregation that implies ownership.
 Usage
Aggregation is used when one object uses another object. Composition is used when one object owns another object.
Affect on Objects
In aggregation, destroying the owning object will not affect the containing object. In composition, destroying the owning object will affect the containing object.

Summary – Aggregation vs Composition in Java

Aggregation and Composition are two concepts in OOP. The “has-a” relationship describes that one object can use another object. Aggregation is an association between two objects that describes the “has-a” relationship. Composition is a more specific type of aggregation that implies ownership.  The difference between aggregation and composition in Java is that, if the contained object can exist without the existence of the owning object it is an aggregation and if the contained object cannot exist without the existence of the owning object, it is a composition.

Reference:

1.“Association, Composition and Aggregation in Java.” GeeksforGeeks, 8 Feb. 2018. Available here 
2.“Aggregation in Java – Javatpoint.” Java Point. Available here