Reputation: 59
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.
Upvotes: 5
Views: 7310
Reputation: 11
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.
Upvotes: 1
Reputation: 390
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...
Upvotes: 6