Association

Basic relationship between classes

Overview

Association represents a relationship between two or more classes where objects of one class are connected to objects of another class. It's a "uses-a" or "knows-a" relationship.

Association is the most general form of relationship and can be:

  • Unidirectional: One class knows about the other
  • Bidirectional: Both classes know about each other

Key Concepts

Objects are connected but independent

Each object has its own lifecycle

Can be one-to-one, one-to-many, or many-to-many

Represented by a simple line in UML

Weakest form of relationship

Code Example

java
1// Association: Teacher and Student know about each other
2// but exist independently
3
4public class Student {
5    private String name;
6    private String studentId;
7    private List<Teacher> teachers; // Association with Teacher
8    
9    public Student(String name, String studentId) {
10        this.name = name;
11        this.studentId = studentId;
12        this.teachers = new ArrayList<>();
13    }
14    
15    public void addTeacher(Teacher teacher) {
16        teachers.add(teacher);
17    }
18    
19    public void attendClass(Teacher teacher) {
20        System.out.println(name + " is attending " + teacher.getSubject() + " class");
21    }
22}
23
24public class Teacher {
25    private String name;
26    private String subject;
27    private List<Student> students; // Association with Student
28    
29    public Teacher(String name, String subject) {
30        this.name = name;
31        this.subject = subject;
32        this.students = new ArrayList<>();
33    }
34    
35    public void addStudent(Student student) {
36        students.add(student);
37    }
38    
39    public void teach() {
40        System.out.println(name + " is teaching " + subject + " to " + students.size() + " students");
41    }
42    
43    public String getSubject() {
44        return subject;
45    }
46}
47
48// Usage - both can exist independently
49public class Main {
50    public static void main(String[] args) {
51        Teacher mathTeacher = new Teacher("Dr. Smith", "Mathematics");
52        Teacher physicsTeacher = new Teacher("Dr. Johnson", "Physics");
53        
54        Student alice = new Student("Alice", "S001");
55        Student bob = new Student("Bob", "S002");
56        
57        // Creating associations
58        alice.addTeacher(mathTeacher);
59        alice.addTeacher(physicsTeacher);
60        
61        mathTeacher.addStudent(alice);
62        mathTeacher.addStudent(bob);
63        
64        // Both exist independently - deleting teacher doesn't delete student
65        mathTeacher.teach();
66        alice.attendClass(physicsTeacher);
67    }
68}

Teacher and Student are associated but independent. Deleting a Teacher doesn't affect Student objects and vice versa.

Real-World Example

  • Doctor and Patient: A doctor treats patients, patients visit doctors, but both exist independently
  • Customer and Product: A customer can buy products, but products exist without customers
  • Employee and Project: Employees work on projects, but both have independent existence

Best Practices

  • 1.

    Use association when objects are related but independent

  • 2.

    Clearly define cardinality (1:1, 1:N, N:M)

  • 3.

    Consider navigation direction (who needs to know whom)

  • 4.

    Don't confuse with composition or aggregation

💡 Interview Tips

  • Explain that association is a "uses-a" relationship

  • Both objects have independent lifecycles

  • Give Teacher-Student or Doctor-Patient example

  • Know UML notation (simple line)

  • Distinguish from stronger relationships

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In
Association - Class Relationships | LLD | Revise Algo