• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Objects in a system rarely exist in isolation — they need to interact, communicate, and reference each other to accomplish tasks. Without a formal way to define relationships between classes, the code would have no structure for how objects find and interact with each other, leading to tightly coupled spaghetti.

Core Idea

Association is an OOP concept that defines a relationship between two or more classes that are connected to each other. It represents how objects interact with each other and communicate. In association, objects of one class are related to objects of another class, but they can exist independently. Association does not imply ownership — it simply means there is a structural or behavioral link between the classes.

How It Works

Association is implemented as a field reference: one class holds a reference to another class. The referenced object is passed in (typically via constructor or method parameter) rather than created inside the class. Because the lifecycle is independent, either object can be garbage collected without affecting the other. Association is the most general form of relationship; aggregation and composition are specialized subtypes.

Visual Explanation

java_association Teacher Class: Teacher - String name - List<Student> students teach() Student Class: Student - String name - List<Teacher> teachers learn() Teacher->Student teaches Independent Both exist independently Teacher can exist without Student Student can exist without Teacher Student->Teacher taught by

Semantic Network

semantic_association THIS Association AGG Aggregation THIS--AGG builds into COMP Composition THIS--COMP builds into OOP OOP in Java THIS--OOP related CLASS Class THIS--CLASS built from

Key Properties

  • Bi-directional or uni-directional: Both classes may know about each other, or only one may hold the reference
  • Independent lifecycles: Objects can exist without each other
  • No ownership: Neither class “owns” the other — it’s a peer relationship
  • Most general form: Association is the broadest type of class relationship
  • Multiplicity: Can be one-to-one, one-to-many, many-to-many

Connections

  • Builds into: Java Aggregation — a weaker form of association with “has-a” semantics
  • Builds into: Java Composition — a stronger form of association with ownership
  • Contrasts with: Java Inheritance — association is a “uses-a” relationship; inheritance is an “is-a” relationship
  • Related: Java Encapsulation — well-encapsulated classes form clean associations

Edge Cases & Gotchas

  • Association vs Dependency: Association is a structural relationship (field reference); dependency is a temporary relationship (method parameter)
  • Circular references: Bidirectional associations can create circular references, complicating garbage collection and serialization
  • Navigability: Not all associations need to be bidirectional — uni-directional reduces coupling