An association represents a bi-directional relationship between two classes. It indicates that objects of one class are connected to objects of another class.
It is the most generic relationship in UML and often forms the foundation for other relationships.

Direction of Association:
- An association can be unidirectional, where only one class is aware of the relationship
- Association can also be bidirectional, where both classes are aware of each other.
- For example, a
Studentmay know theirTeacher, and theTeachermay also know the list of students.
Multiplicity
Associations can also define how many instances of one class relate to another. This is expressed in terms of multiplicity, such as one-to-one, one-to-many, or many-to-many. Multiplicity is written at the ends of the association line (e.g., 1, 0..*, 1..*).
Nature of Relationship:
Unlike stronger relationships like composition, association simply shows a logical connection between objects. The existence of one object does not depend on the other.
Understanding association using an example:
Let's consider a simple system for managing students and teachers. In this system, we have two main entities: Teacher and Student. Each Teacher can teach multiple Students, and each Student can learn from multiple Teachers. This relationship between Teacher and Student represents an association.

The Teacher class can be considered the source class because it maintains references to multiple instances of the Student class. The Student class would be considered the target class because it is associated with one or more Teachers.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student {
public:
string name;
Student(const string& n) : name(n) {}
};
class Teacher {
public:
string name;
// association: teacher 'uses' (knows) students
vector<Student*> students;
Teacher(const string& n) : name(n) {}
void addStudent(Student* s) {
students.push_back(s);
}
void printStudents() const {
cout << "Teacher " << name << " has students:\n";
for (const auto* s : students) {
cout << " - " << s->name << "\n";
}
}
};
int main() {
Student alice("Alice");
Student bob("Bob");
Teacher mrSmith("Mr. Smith");
mrSmith.addStudent(&alice);
mrSmith.addStudent(&bob);
mrSmith.printStudents();
return 0;
}
import java.util.ArrayList;
import java.util.List;
class Student {
public String name;
Student(String n) {
name = n;
}
}
class Teacher {
public String name;
// association: teacher 'uses' (knows) students
public List<Student> students = new ArrayList<>();
Teacher(String n) {
name = n;
}
public void addStudent(Student s) {
students.add(s);
}
public void printStudents() {
System.out.println("Teacher " + name + " has students:");
for (Student s : students) {
System.out.println(" - " + s.name);
}
}
}
public class Main {
public static void main(String[] args) {
Student alice = new Student("Alice");
Student bob = new Student("Bob");
Teacher mrSmith = new Teacher("Mr. Smith");
mrSmith.addStudent(alice);
mrSmith.addStudent(bob);
mrSmith.printStudents();
}
}
class Student:
def __init__(self, n):
self.name = n
class Teacher:
def __init__(self, n):
self.name = n
# association: teacher 'uses' (knows) students
self.students = []
def addStudent(self, s):
self.students.append(s)
def printStudents(self):
print("Teacher " + self.name + " has students:")
for s in self.students:
print(" - " + s.name)
def main():
alice = Student("Alice")
bob = Student("Bob")
mrSmith = Teacher("Mr. Smith")
mrSmith.addStudent(alice)
mrSmith.addStudent(bob)
mrSmith.printStudents()
if __name__ == "__main__":
main()
class Student {
constructor(n) {
this.name = n;
}
}
class Teacher {
constructor(n) {
this.name = n;
// association: teacher 'uses' (knows) students
this.students = [];
}
addStudent(s) {
this.students.push(s);
}
printStudents() {
console.log("Teacher " + this.name + " has students:");
for (let s of this.students) {
console.log(" - " + s.name);
}
}
}
// Example usage
const alice = new Student("Alice");
const bob = new Student("Bob");
const mrSmith = new Teacher("Mr. Smith");
mrSmith.addStudent(alice);
mrSmith.addStudent(bob);
mrSmith.printStudents();
Output
Teacher Mr. Smith has students: - Alice - Bob