Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Aggregation and Composition?

Tags:

java

oop

uml

What is the difference between aggregation and composition? I haven't understood it in my classroom, if you could please give me some examples or some more explanations.

like image 637
C. Cretan Avatar asked Jul 24 '26 04:07

C. Cretan


2 Answers

Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist.

Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don't exist separate to a House. Read more...

like image 198
Mihail Avatar answered Jul 26 '26 17:07

Mihail


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).

Aggregation and Composition are subsets of association meaning they are specific cases of association. In both aggregation and composition object of one class "owns" object of another class. But there is a subtle difference:

The difference between them is that in aggregation relationship the child object can exists without the parent object, whilst in composition relationship the child object can't exist without the parent object. There is a little example: -For composition: suppose you have the parent object "car" and the child object "engine". The car can't exist without an engine. -For aggregation: suppose you have the parent object "ice-cream" and the child object "topping". The ice-cream can have topping, but it can exists also without topping.

like image 33
Razvan Birhoata Avatar answered Jul 26 '26 18:07

Razvan Birhoata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!