Aggregation & Composition in OOPs

  • Aggregation and composition in real project define HAS-A relationship.
  • Aggregation differs from ordinary composition in that it does not imply ownership. 
  • In composition, when the owning object is destroyed, so are the contained objects. 
  • In aggregation, this is not necessarily true.
 For example, a university owns various departments (e.g., Physics, chemistry), and each department has a number of professors.

Now due to some reasons, if the university closes, the departments will no longer exist, but the professors in those departments will. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors.

  • Aggregation represents ownership or a whole/part relationship.
  • The composition represents an even stronger form of ownership. With composition, we get a coincident lifetime of part with the whole. The composite object has sole responsibility for the disposition of its parts in terms of creation and destruction. In implementation terms, the composite is responsible for memory allocation and de-allocation. An object may be part of only one composite at a time. If the composite is destroyed, it must either destroy all its parts or else give responsibility for them to some other object. A composite object can be designed with the knowledge that no other object will destroy its parts.
  • Composition – One class has another class.
  • Aggregation – One class is a kind of another class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s