DEV Community

Cover image for Association in OOP
Ahmad Mukahal
Ahmad Mukahal

Posted on

1

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.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay