Home »
Java »
Java Articles
Java - Differences Between Aggregation and Composition
By Shahnail Khan Last updated : March 22, 2024
In Java programming, when designing classes and their relationships, two important concepts often come into play: Aggregation and Composition. These concepts help in modelling real-world scenarios and defining how objects interact with each other. In this article, we will understand the difference between Aggregation and Composition in detail.
What is Aggregation in Java?
Aggregation is a relationship between two classes where one class contains a reference to another class, but both classes can exist independently. In other words, it's a "has-a" relationship where the child object can exist without the parent object.
Example of Aggregation in Java
Let's take an example to understand this better.
Consider a Library and its collection of Books. A Library contains multiple Books, but a Book can exist outside of the library. Even if the library is closed or destroyed, the Books still exist independently.
class Library {
private List < Book > books;
// Constructor, methods, etc.
}
class Book {
// Attributes and methods of a book
}
In this example, the Library class aggregates Book objects, as it contains a reference to a collection of Books. However, Books can exist on their own without being part of any Library.
What is Composition in Java?
Composition is a stronger form of Aggregation where the child object is an integral part of the parent object. In Composition, the child object cannot exist without the parent object. It's a "whole-part" relationship.
Example of Composition in Java
Let's take an example to understand this better.
Let's take a Car and its Engine. A Car is composed of an Engine. If the Car is destroyed, the Engine ceases to exist as well.
class Car {
private Engine engine;
// Constructor, methods, etc.
}
class Engine {
// Attributes and methods of an engine
}
In this example, the Car class composes an Engine object. The Engine is an integral part of the Car; it cannot exist independently. If the Car is destroyed, the Engine is also destroyed or becomes unusable.
Differences Between Aggregation and Composition in Java
Criteria |
Aggregation |
Composition |
Independence |
Child objects can exist independently. |
Child objects are dependent on the parent. |
Relationship
|
Represents a "has-a" relationship. |
Represents a "whole-part" relationship. |
Strongness
|
Weak |
Strong |
Lifecycle
|
Independent |
Dependent on the parent. |
Ownership
|
Parent object doesn't own the child. |
Parent object owns the child. |
Creation/Destruction
|
Child objects can be created/destroyed independently. |
Created/Destroyed along with the parent. |
Flexibility
|
Offers more flexibility. |
Less flexible. |
Granularity
|
Higher level of abstraction. |
Lower level of abstraction. |
Code Reusability
|
Promotes code reusability. |
Encourages encapsulation and code reuse. |
Example
|
Library containing books. |
Car containing an engine. |