Association, Aggregation, Composition

Share

 

These terms are the building blocks of Object Oriented programming language (OOPS) concepts and should be known to a programmer working in OOPS as a must thing. But as a matter of fact, not many people are aware of these terms. Its my sincere effort to bring forth the simplicity of few of OOPS concepts before you.

Association

Association is a relationship between two classes (objects). Whenever a class use another class, there exists a relationship between the two classes, this relationship is known as association.

Example: Their exist association between students and teachers. There is no owner object. Students and Teachers have their own independent life cycles but their exist a relationship between a student and a teacher.

Association talks about relationship between objects in general.  Aggregation is a special form of association. Composition is a special form of aggregation.

Aggregation

Its a special case of association. If there exists association (relationship) between two objects; and one object seems containing another object (having ownership of other object), there exists Aggregation between objects. In other words, when an object ‘has-a’ another object, then there exists an aggregation between them. Aggregation is also called a “Has-a” relationship.

Note: If you delete the parent object, even then the child object may exist. One object can contain the other, but there is no restriction that the composed object has to exist in order to have existence of child object.

Example: Their exist aggregation between a Department and Teacher. Department has a Teacher (in other words, the department contains teachers, or department has a ownership of teacher). Even if you delete the department, a teacher would exist.

UML notation:

Composition

Composition is a special case of aggregation. The more restricted form of aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.

UML notation:

Example: A Company contains Employees. There exists  composition between Company and Employees (since, an employee can not exist without a company).

 

Difference between aggregation and composition

Association talks about relationship between two classes (objects) in general. Any type of relationship between two objects is called association. Aggregation is more restricted form of association. When a object has a ownership of another object, there exists aggregation between objects. However, this ownership does not mean that: without Parent object child object would not exist. Composition is again the more restricted form of aggregation. When a object has a ownership of another object and without Parent object, child object can not exist, there exists composition.