Association
Definition
Association is a relationship between two independent classes or objects in which one object is connected to, communicates with, or uses another object without implying ownership or strong dependency.
This relationship can be:
One-to-one
- : one object is related to exactly one other object
One-to-many
- : one object is related to several objects
Many-to-many
- : multiple objects are related to multiple other objects
Unlike inheritance, association does not mean “is-a” relationship. Unlike composition, it does not mean one object completely owns or controls the lifecycle of another.
Main Content
1. Basic Nature of Association
Association represents a “knows-a” or “uses-a” relationship
It shows that objects are connected in a meaningful way. For example, a Teacher class may be associated with a Classroom class because teachers work in classrooms. The classes remain separate and independent, but they cooperate to perform tasks.
Objects in association can exist independently
In association, one object does not need to be created or destroyed because of the other. For instance, a Student can exist even if a School object is removed in the program model. This independence is what makes association more flexible than stronger relationships like composition.
Example:
Teacher -------- teaches --------> Student
In this example:
- Teacher and Student are different entities
- Teacher is not a kind of Student
- Student is not owned permanently by Teacher
- They are simply connected through teaching
2. Types of Association
One-to-One Association
In this type, one instance of a class is linked to exactly one instance of another class.
Example: A Person may have one Passport.
Person 1 -------- 1 Passport
This relationship is common when one object has a direct link to another object.
One-to-Many and Many-to-Many Association
In one-to-many association, one object relates to many objects.
Example: One Teacher teaches many Students.
In many-to-many association, many objects from one class can relate to many objects of another class.
Example: Many Students enroll in many Courses.
These relationships are common in databases and software systems where multiple entities interact.
Example:
Teacher 1 -------- * Student
Student * -------- * Course
Important idea:
- The numbers show multiplicity, meaning how many objects can participate in the relationship.
3. Association in UML and Programming
UML representation uses a line between classes
In Unified Modeling Language (UML), association is drawn as a solid line connecting two classes. The line may include arrows, labels, and multiplicity indicators to explain direction and quantity.
Example:
Customer -------- places -------- Order
Programming implementation often uses object references
In code, association is typically implemented by storing a reference or link to another object. For example, a Car class may store a reference to a Driver object. This allows the objects to interact during execution.
Example in pseudocode:
class Teacher
has reference to Student
This does not mean the teacher creates or destroys the student; it simply means the teacher knows which student is being taught.
Working / Process
1. Identify the related objects
- Decide which classes or objects in the system have a meaningful relationship.
- Example:
DoctorandPatient,LibraryandBook,UserandAccount.
2. Determine the relationship type and multiplicity
- Decide whether the relationship is one-to-one, one-to-many, or many-to-many.
- Example: One
Doctormay treat manyPatients.
3. Connect the objects through references or links
- In UML, draw a line between the classes.
- In code, store the related object(s) as attributes, references, lists, or collections.
Example process diagram:
Doctor ---- treats ---- Patient
| |
|--- reference -------|
This means:
- Doctor can access patient information
- Patient can exist separately
- The relation is only a connection, not ownership
Advantages / Applications
Models real-world relationships naturally
Association is very useful because most real systems consist of related entities. It helps represent real-life connections accurately, such as a Student attending a School or an Employee working in a Department.
Promotes flexible and reusable design
Since associated objects remain independent, changes in one class do not usually force major changes in the other. This makes the software easier to maintain, extend, and reuse.
Widely used in software engineering and databases
Association appears in UML class diagrams, object-oriented design, relational database design, and enterprise applications. It is essential for building systems like banking software, hospital management systems, e-commerce platforms, and educational systems.
Summary
- Association is a relationship between independent objects
- It is used to show connection, cooperation, or usage
- Important terms to remember: association, multiplicity, object reference, UML relationship