Pages

SPONSORED

Composition and Aggregation

Composition
Composition expresses relationship between objects. It is an association in which one class belongs to a collection. The containing object is responsible for the life time of the object it holds within it. The containing object cannot exist without the main object. It expresses the “has a” relationship among the objects

Example:  Office has a reception, Student has a name, Car has a tyre etc.

Aggregation
It is relationship which is just a reference between objects. The objects are independent of each other.

Example: Chair and person, Chair does not own the person.
Employee and address, address stays even after employee is destroyed.

The table below lists the key differences between composition and aggregation

Composition
Aggregation
Relationship where a part cannot exist without whole
Relationship where a part can exist without whole
Stronger relationship
Weaker relationship
Main object owns the containing objects
Objects are independent of each other
Main object instantiated the containing objects
Objects within the relationship are instantiated independently
Containing objects are destroyed when main object is destroyed
If one object is destroyed other object can survive.




No comments:

Post a Comment