Aggregation and Composition (Has-a relationship)

 Has-a relationship : 

  • Has- a relationship is also known as composition and aggregation.
  • The main advantage of Has-a relationship is re-usability of the code.
  • There is no specific keyword to implement Has-a relationship but most of the time we are depending upon new keyword.

 

Composition :

Without existing container object if there is no chance of existing the contained objects then container and contained objects are strongly associated and this association is called composition.

Example : University containing various departments without existing university there is no chances of existing departments hence university and departments are strongly associated with each other and this strong association is called composition.

 

Aggregation :

without existing the container object if there is a chance of existing the contained objects then container and the contained objects are weakly associated with each other and this weak association is called aggregation.

Example : Department consists of several professors. Without existing department there may be the chances of existing professor objects. Hence department and professors objects are weakly associated and this is weak association.

 

Leave a Reply