Aggregation and Composition in Java Example

Association is a relationship between two separate classes which can be of any type say one to one, one to may etc. It joins two entirely separate entities.

Association further divides into two types –

  • Aggregation

  • Composition

Aggregation and Composition both justify the concept of Association the only difference is Composition is more restrictive as compare the Aggregation.


Aggregation in Java

Aggregation defines the one directional relationship between the class. It represents a Has-A relationship. It is less restrictive in nature i.e suppose A and B are associated with each other, then B can exist without being associated with A, it means if Class A destroyed then B can exist.

Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.

Here Teacher has a department.

Output :

Mirilla Knightly
Computer Science


Composition in Java

The composition is the restricted part of the Association, In composition classes are highly dependent on each other. When an object contains the other object, If the contained object cannot exist without the existence of container object, then it is called composition.

Let’s take the example of House and Kitchen, if the house gets destroyed then the kitchen will also destroy. This type of association is called composition.


Selenium Testing Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply