Monday, November 11, 2013

Difference between Aggregation and Composition in java.

Difference between Aggregation and Composition in java.

Aggregation means weak relationship. A container object can have the contained object, but its not mandatory with out contained object , container object will not exist.

Best Example is, A student and library

Composition means strong relationship, A container object will not exist without the contained object.
Best Example is, A student and Book

In composition (Person, Heart, Hand), "sub objects" (Heart, Hand) will be destroyed as soon as Person is destroyed.

In aggregation (City, Tree, Car) "sub objects" (Tree, Car) will NOT be destroyed when City is destroyed.

The bottom line is, composition stresses on mutual existence, and in aggregation, this property is NOT required.