Introduction
In UML people generally get confused between terms Composition and Aggregation and some time with Inheritance. Below these three relations are explained with UML diagram to example the difference.
Composition:
When one (A) class have reference of instance of another class (B) and all the access that instance is controlled by container class (A), then this relation is called composition. A is a composition of B. For example one company (class A) can have many departments. So Company is composition of department (Class B). Department do not have its identity without company, here departments are owned by company so this type of relation is also called "owns a" relation. Below is the UML representation.

Aggregation:
When a class A contains a reference of class B which also have instance and accessible outside A, then this relationship is called aggregation and B is aggregation of A. For example department in above example can have employees but same employ can work for other department also witness same role. So different departmental have instance of employee (B) and employee can exist independent of the department(A). We can say A class has a object of class B. This kind of relation are also called "has a" relation. Below is the UML representation.

Inheritance:
When a class A contains all the members of class B, it is call A extends B or A inherits B. In above example employee class inherits a person class. Such kind of relations are also called "is a" relation. Below is the UML representation.

Complete class diagram

No comments:
Post a Comment