Association is a relationship in which one object knows about, communicates with or works with another object.
The objects remain independent. Neither object necessarily owns the other or controls its lifetime.
Association means that two objects are connected because they interact or share information.
Simple Example
Consider a teacher and a student:
class Teacher {
void teach(Student student) {
student.learn();
}
}
class Student {
void learn() {
System.out.println("Learning");
}
}
The Teacher interacts with a Student, but both can exist independently.
Deleting the teacher does not automatically delete the student.
UML Notation
Association is represented by a solid line between two classes:
An arrow can optionally show navigation direction:
This means Teacher knows about or uses Student.
Association Multiplicity
Multiplicity describes how many objects can participate in the relationship.
| Notation | Meaning |
|---|---|
1 | Exactly one |
0..1 | Zero or one |
* | Any number |
1..* | One or more |
0..* | Zero or more |
For example:
This indicates that one teacher can teach zero or more students.
Types of Association
One-to-one
One object is associated with one other object.
Person - Passport
One-to-many
One object is associated with multiple objects.
Teacher - Students
Many-to-one
Several objects are associated with one object.
Employees - Department
Many-to-many
Multiple objects on both sides can be associated.
Students - Courses
Unidirectional Association
Only one class knows about the other.
class Order {
private Customer customer;
}
Order knows about Customer, but Customer does not necessarily store a reference to Order.
Use unidirectional association when only one side needs to navigate the relationship.
Bidirectional Association
Both classes know about each other.
class Teacher {
private List<Student> students;
}
class Student {
private Teacher teacher;
}
Bidirectional relationships can be useful, but they create tighter coupling. Both sides must also remain synchronized.
For example, adding a student to a teacher may require updating both objects.
Main Characteristics
- The objects can exist independently.
- One object may store a reference to another.
- The relationship can be temporary or long-lasting.
- It may be unidirectional or bidirectional.
- It can represent one-to-one, one-to-many or many-to-many connections.
- It does not automatically imply ownership.
- It does not control the lifetime of the related object.
Association vs Dependency
Association normally represents a structural connection.
class Order {
private Customer customer;
}
The Order keeps a reference to the Customer.
A dependency is usually weaker and more temporary:
class ReportService {
void generate(Printer printer) {
printer.print();
}
}
ReportService only uses Printer while executing a method.
A useful distinction is:
Association usually means “has access to,” while dependency often means “temporarily uses.”
Association vs Aggregation
Association only indicates that objects are connected.
Aggregation adds a weak whole-part meaning.
For example:
Doctor - Patient Association
Team <>- Player Aggregation
Every aggregation is a form of association, but not every association is an aggregation.
When to Use Association
Use association when:
- Two objects need to communicate.
- One object needs access to another object.
- The objects have independent lifetimes.
- There is no strong ownership relationship.
- The connection is meaningful within the domain.
Examples include:
- A doctor treats patients.
- A customer places orders.
- A driver operates a vehicle.
- A student enrols in courses.
- An employee works with a manager.
Common Mistakes
Assuming association means ownership
A stored reference does not automatically mean one object owns the other.
Making every relationship bidirectional
Bidirectional relationships increase coupling and synchronization work. Use them only when both navigation directions are required.
Confusing association with inheritance
Association represents has-a or works-with behaviour:
Teacher has Students.
Inheritance represents an is-a relationship:
Manager is an Employee.
Ignoring multiplicity
The number of related objects affects the class structure.
A one-to-one relationship may use one reference, while a one-to-many relationship usually requires a collection.
Creating unnecessary associations
If one class does not genuinely need to know about another, adding a reference creates avoidable coupling.
Interview Explanation
A short interview answer:
Association is a relationship in which two independent objects know about or interact with each other. It does not necessarily represent ownership, and destroying one object does not automatically destroy the other.
A stronger answer:
Association represents a structural connection between classes. It can be unidirectional or bidirectional and can have one-to-one, one-to-many or many-to-many multiplicity. The associated objects usually have independent lifetimes, which distinguishes general association from composition.
Interview Questions
- What is association in object-oriented design?
- Does association imply ownership?
- What is the difference between unidirectional and bidirectional association?
- What does multiplicity represent?
- How does association differ from dependency?
- How does association differ from aggregation?
- Can two objects in an association exist independently?
- When should a bidirectional association be avoided?
Key Takeaways
- Association connects objects that interact or know about each other.
- Associated objects usually have independent lifetimes.
- It does not automatically indicate ownership.
- It may be unidirectional or bidirectional.
- It supports one-to-one, one-to-many and many-to-many relationships.
- UML represents it with a solid line.
- Aggregation and composition are more specific forms of association.
- Prefer the weakest and simplest relationship that satisfies the design.