AlgoMaster Logo

Association

Ashish

Ashish Pratap Singh

In the real world, nothing exists in a vacuum. A doctor has patients, a driver has a car, and a student enrolls in courses. These are all relationships—connections that define how different entities interact.

When we build software using Object-Oriented Programming (OOP), our goal is to model this real world.

So, how do we represent these connections between our objects?

In this chapter, we will explore the most fundamental and common of these relationships: Association.

1. What is Association?

Association represents a relationship between two classes where one object uses, communicates with, or references another.

This relationship answers the question:

“Does one object need to know about the existence of another object to perform its responsibilities?”

If the answer is yes, there is an association between them.

Key Characteristics of Association:

  • Association reflects a "has-a" or "uses-a" relationship.
  • Associated objects are loosely coupled and can exist independently of one another.
  • The association can be unidirectional or bidirectional, and can follow different multiplicity patterns (1-to-1, 1-to-many, etc.).

2. Types of Association

Association comes in multiple forms depending on:

  • Direction: who knows about whom
  • Multiplicity: how many objects are connected

Based on Direction:

  • Unidirectional:  Only one class has a reference to the other
  • Bidirectional: Both classes know about each other

Based on Multiplicity:

  • One-to-One: Each object is associated with exactly one object of the other class
  • One-to-Many: One object can be associated with many other objects
  • Many-to-Many:  Objects from both classes can be associated with multiple objects from the other

These forms are often combined. For example, a bidirectional one-to-many relationship exists between an Author and multiple Books.

3. Examples

Unidirectional Association

In a unidirectional association, only one class is aware of the other.

Example: A Car has a Driver, but the Driver doesn't know about the Car.

Explanation:

  • The Car knows about the Driver and interacts with it.
  • The Driver class is completely unaware of the Car.
  • This models a one-way interaction and is useful when only one class needs access to the other.

Bidirectional Association

In a bidirectional association, both classes are aware of each other and can reference one another.

Example: An Author writes multiple Books, and each Book knows its Author.

In this bidirectional association:

  • Author has a list of Books.
  • Each Book stores a reference to its Author.

This is a two-way communication channel between the classes.

4. UML Representation

In UML class diagrams, association is represented by a solid line between two classes:

Multiplicity can also be marked:

  • 1 for one
  • * for many

5. When to Use Association in OOP

Use association when:

  • Two classes need to collaborate.
  • One class needs to send a message to or query another.
  • You want to express relationships without implying ownership.
  • You need flexibility—both objects should be reusable and independently manageable.