Introduction:
The association describes the relationships or connections between various objects. It represents a general relationship between objects without any strong dependency.
-
The association has two forms: Composition and Aggregation.
-
Objects associated with each other communicate using references to use their properties and characteristics.
Let us look into the types of Association and their role in object-oriented programming:
One-to-One Association:
One-to-Many: One object is associated with exactly one object.
Example: A person is linked with exactly one social security number and vice versa.
Here is an example of how a person can be linked with exactly one social security number and vice versa in Java:
public class Person {
private String name;
private SocialSecurityNumber ssn;
public Person(String name, SocialSecurityNumber ssn) {
this.name = name;
this.ssn = ssn;
}
// Other methods and attributes
// Getter and setter for name and ssn
}
public class SocialSecurityNumber {
private String number;
private Person person;
public SocialSecurityNumber(String number, Person perso
n) {
this.number = number;
this.person = person;
}
// Other methods and attributes
// Getter and setter for number and person
}
In this example, the Person class has a reference to a Social Security Number object, and the Social Security Number class has a reference to a Person object. This represents a one-to-one association between person and their social security number.
One-to-Many Association:
One-to-Many: One object can be associated with many objects.
Example: A person can have multiple bank accounts, but a single account is linked to a single person.
import java.util.ArrayList;
import java.util.List;
public class Person {
private String name;
private List<BankAccount> bankAccounts;
public Person(String name) {
this.name = name;
this.bankAccounts = new ArrayList<>();
}
public void addBankAccount(BankAccount account) {
bankAccounts.add(account);
}
public void removeBankAccount(BankAccount account) {
bankAccounts.remove(account);
}
// Other methods and attributes
// Getter and setter for name and bankAccounts
}
public class BankAccount {
private String accountNumber;
private Person owner;
public BankAccount(String accountNumber, Person owner)
{
this.accountNumber = accountNumber;
this.owner = owner;
}
// Other methods and attributes
// Getter and setter for accountNumber and owner
}
In this example, the Person class has a list of Bank Account objects to represent the multiple bank accounts associated with a person. Each Bank Account object has a reference to the owner, which is a Person object. This represents a one to many association between a person and their bank accounts.
Many-to-Many Association: A variable number of objects belonging to a class may be associated with a variable number of objects belonging to another class.
Example: A person can have multiple home addresses, and a home address may be linked to multiple people.
import java.util.ArrayList;
import java.util.List;
public class Person {
private String name;
private List<HomeAddress> homeAddresses;
public Person(String name) {
this.name = name;
this.homeAddresses = new ArrayList<>();
}
public void addHomeAddress(HomeAddress address) {
homeAddresses.add(address);
address.addPerson(this);
}
public void removeHomeAddress(HomeAddress address) {
homeAddresses.remove(address);
address.removePerson(this);
}
// Other methods and attributes
// Getter and setter for name and homeAddresses
}
public class HomeAddress {
private String address;
private List<Person> residents;
public HomeAddress(String address) {
this.address = address;
this.residents = new ArrayList<>();
}
public void addPerson(Person person) {
residents.add(person);
}
public void removePerson(Person person) {
residents.remove(person);
}
// Other methods and attributes
// Getter and setter for address and residents
}
In this example, the Person class has a list of Home Address objects to represent the multiple home addresses associated with a person. Each Home Address object has a list of residents, which are Person objects. By adding a person to a home address and vice versa, we establish a many-to-many association between people and home addresses.
Many-to-One Association: The opposite of a one-to-many association is a many-to-one association. Here,one object from one class is linked to several objects from another class.
Example: Consider a school with multiple classes, and in each class, many students are studying together. This is a Many-to-one association.
import java.util.ArrayList;
import java.util.List;
public class School {
private String name;
private List<Classroom> classrooms;
public School(String name) {
this.name = name;
this.classrooms = new ArrayList<>();
}
public void addClassroom(Classroom classroom) {
classrooms.add(classroom);
classroom.setSchool(this);
}
public void removeClassroom(Classroom classroom) {
classrooms.remove(classroom);
classroom.setSchool(null);
}
// Other methods and attributes
// Getter and setter for name and classrooms
}
public class Classroom {
private String name;
private School school;
private List<Student> students;
public Classroom(String name) {
this.name = name;
this.students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
student.setClassroom(this);
}
public void removeStudent(Student student) {
students.remove(student);
student.setClassroom(null);
}
public void setSchool(School school) {
this.school = school;
}
// Other methods and attributes
// Getter and setter for name, school and students
}
public class Student {
private String name;
private Classroom classroom;
public Student(String name) {
this.name = name;
}
public void setClassroom(Classroom classroom) {
this.classroom = classroom;
}
// Other methods and attributes
// Getter and setter for name and classroom
}
In this example, we have three classes: School , Classroom , and Student .
-
The School class represents a school and has a list of Classroom objects associated with it.
-
The Classroom class represents a classroom and has a reference to the School it belongs to, as well as a list of Student objects associated with it.
-
The Student class represents a student and has a reference to the Classroom they belong to.
To establish the many-to-one association:
-
When adding a classroom to the school, we call the setSchool() method of the classroom to set the reference to the school.
-
When adding a student to a classroom, we call the setClassroom() method of the student to set the reference to the classroom.
This way, we can have multiple classrooms in a school, and each classroom can have many students studying together.