Wednesday, December 7, 2011

Association, Aggregation & Composition

Association : An association is  the relation between two objects. In other words its connectivity between two objects. Aggregation and compositions are form of association. 
Aggregation: This is a part of a whole relationship where a part can exist without a whole.
Aggregation is a weak association.  Also called “has a” relationship.
Composition : This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted.
Composition is a Strong association.
Example:
A House contains Room and TV. Relationship between House and TV is aggregation and relation between House and Room is composition.
A TV can exist without a House-> Weak association (Aggregation)
A Room cannot exist without a House -> Strong association (Composition).

Java Example :
Aggregation

Class LPG{
}

Class Car{
Car(LPG objLPG) //Object is passed from outside   - weak relationship –LPG can exist without a car
{
}
}

Composition
Class Engine{}
Class Car{
                Car(){
                Engine objEng = new Engine();  //lives within car object   - Strong relationship: Engine cannot exist without car.
}
}