Quantcast
Viewing all articles
Browse latest Browse all 28

Aggregation Vs Composition

Just for people who got the same question in their Java interview.. I  got this question from many people during interview.. I don't ask the same to any one in an interview though, I wanted to share this to anyone who is looking for an answer for this........


Aggregation is an association in which one class belong to a collection. This is a part of a whole relationship where a part can exist without a whole.

Eg. A line item is a whole and product is a part. If a line item is deleted the corresponding product need not be deleted.

So Aggregation is a weaker relationship

Composition is a relation in which one class belong to a collection. This is a part of a whole relationship where a part cannot exist without a whole. It a whole is deleted then all parts are deleted.

Eg. An order is a whole and line items are part. If an order is deleted then all corresponding line items for that order should be deleted.

So composition has a stronger relationship.

Viewing all articles
Browse latest Browse all 28

Trending Articles