In Java, an association is a relationship between two classes in which one class (the "source" class) has a reference to an instance of the other class (the "target" class). Association can be one-way (where the source class has a reference to the target class, but the target class does not have a reference to the source class) or two-way (where both classes have references to each other).
There are three types of association:
One-to-one association: This type of association represents a relationship in which one instance of the source class is associated with exactly one instance of the target class, and vice versa.
One-to-many association: This type of association represents a relationship in which one instance of the source class is associated with multiple instances of the target class, but each instance of the target class is associated with at most one instance of the source class.
Many-to-many association: This type of association represents a relationship in which multiple instances of the source class are associated with multiple instances of the target class, and vice versa.
Association is implemented in Java using instance variables and methods. For example, consider the following classes:
class Student {
// instance variables
private String name;
private int age;
// constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// getter and setter methods
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}
class Course {
// instance variables
private String name;
private int credits;
// constructor
public Course(String name, int credits) {
this.name = name;
this.credits = credits;
}
// getter and setter methods
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getCredits() { return credits; }
public void setCredits(int credits) { this.credits = credits; }
}
In this example, the "Student" class and the "Course" class are two separate classes with no association between them. If you want to create a one-to-many association between these classes, you could modify the "Course" class like this:
class Course {
// instance variables
private String name;
private int credits;
private List<Student> students; // one-to-many association with Student class
// constructor
public Course(String name, int credits) {
this.name = name;
this.credits = credits;
this.students = new ArrayList<>();
}
// getter and setter methods
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getCredits() { return credits; }
public void setCredits(int credits) { this.credits = credits; }
// methods for managing the association
public void addStudent(Student student) { students.add(student); }
public void removeStudent(Student student) { students.remove(student); }
public List<Student> getStudents() { return students; }
}
Now, the "Course" class has a one-to-many association with the "Student" class, which means that one instance of the "Course" class can be associated with multiple instances of the "Student" class.
To create a many-to-many association between these classes, you could modify the "Student" class like this:
class Student {
// instance variables
private String name;
private int age;
private List<Course> courses; // many-to-many association with Course class
// constructor
public Student(String name, int age) {
this.name = name;
this.age = age;
this.courses = new ArrayList<>();
}
// getter and setter methods
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
// methods for managing the association
public void addCourse(Course course) { courses.add(course); }
public void removeCourse(Course course) { courses.remove(course); }
public List<Course> getCourses() { return courses; }
}
Now, both the "Student" class and the "Course" class have a many-to-many association with each other, which means that multiple instances of the "Student" class can be associated with multiple instances of the "Course" class, and vice versa.
I hope this helps! Let me know if you have any other questions.
Comments
Post a Comment