DEV Community

Cover image for Association in OOP
Ahmad Mukahal
Ahmad Mukahal

Posted on

Association in OOP

Association in OOP is a connection or relation between two separate #classes that are set up through their #objects. Association relationship indicates how objects know each other and how they are using each other’s functionality. It can be one-to-one, one-to-many, many-to-one, and many-to-many.

For example, a person can have only one passport. That is a “one-to-one” relationship between person class and passport class.
And so on;

- Two Forms of Association

Composition and Aggregation are the two special forms of association, both of them have a "has-a" relationship. let's check them out:

1- Composition
It simply means that one large object contains the other smaller object. In other words, it’s part or member of the larger object.
For example, a building (large object) #has rooms (smaller object).

2- Aggregation
Aggregation is also a “has-a” relationship, It is like a composition with Minor logical differences:

- *Aggregation vs Composition *

1. Dependency:
Aggregation implies a relationship where the child can exist independently of the parent. For example, Bank and Employee, delete the Bank and the Employee still exist. whereas Composition implies a relationship where the child cannot exist independent of the parent. For example: if the building is destroyed the room is destroyed but the building will still exist!

2. Type of association:
Composition is a strong Association whereas Aggregation is called a weak Association.

Top comments (0)