In Java, association is a fundamental concept in object-oriented programming (OOP) that describes how objects are related to each other. Unlike inheritance (which represents an "is-a" relationship) and composition (which is a "has-a" relationship), association represents a more general "uses-a" relationship between objects.
Association can be categorized into the following types:
1. Unidirectional Association
- In this type of association, one class is aware of another, but not the other way around. This is the most common and simplest type of association.
- For example, if class
A
has a reference to classB
, then classA
is associated with classB
. ButB
doesn't know anything aboutA
.
Example:
class Engine {
void start() {
System.out.println("Engine started");
}
}
class Car {
Engine engine; // Car uses Engine
Car(Engine engine) {
this.engine = engine;
}
void drive() {
engine.start();
System.out.println("Car is driving");
}
}
public class Main {
public static void main(String[] args) {
Engine engine = new Engine();
Car car = new Car(engine);
car.drive(); // Output: Engine started, Car is driving
}
}
In this example, the Car
class is aware of the Engine
class, but the Engine
class has no reference to Car
.
2. Bidirectional Association
- This means both classes in the association know about each other. This is a more complex association where changes in one class may affect the other.
- For instance, class
A
knows about classB
, and classB
also knows about classA
.
Example:
class Person {
String name;
Company company;
Person(String name, Company company) {
this.name = name;
this.company = company;
}
}
class Company {
String companyName;
Person employee;
Company(String companyName, Person employee) {
this.companyName = companyName;
this.employee = employee;
}
}
public class Main {
public static void main(String[] args) {
Company google = new Company("Google", null);
Person john = new Person("John", google);
google.employee = john;
System.out.println(john.name + " works at " + google.companyName);
}
}
In this case, both Person
and Company
are aware of each other, and their relationship is bidirectional.
3. Association Types Based on Cardinality
Association can also be classified based on cardinality, which defines how many objects can participate in the relationship.
a. One-to-One Association
- This type of association is when one object of a class is associated with only one object of another class.
Example:
class Passport {
String passportNumber;
}
class Citizen {
String name;
Passport passport; // One Citizen has one Passport
Citizen(String name, Passport passport) {
this.name = name;
this.passport = passport;
}
}
b. One-to-Many Association
- One object of a class is associated with multiple objects of another class.
Example:
class Department {
String departmentName;
List<Employee> employees; // One department can have many employees
Department(String departmentName) {
this.departmentName = departmentName;
this.employees = new ArrayList<>();
}
}
class Employee {
String name;
Employee(String name) {
this.name = name;
}
}
c. Many-to-One Association
- Many objects of one class can be associated with one object of another class.
Example:
class Order {
int orderID;
Customer customer; // Many orders can belong to one customer
Order(int orderID, Customer customer) {
this.orderID = orderID;
this.customer = customer;
}
}
class Customer {
String name;
Customer(String name) {
this.name = name;
}
}
d. Many-to-Many Association
- This is when many objects of one class are associated with many objects of another class.
Example:
class Student {
String name;
List<Course> courses; // Many students can be enrolled in many courses
Student(String name) {
this.name = name;
this.courses = new ArrayList<>();
}
}
class Course {
String courseName;
List<Student> students; // Many courses can have many students
Course(String courseName) {
this.courseName = courseName;
this.students = new ArrayList<>();
}
}
4. Aggregation
- Aggregation is a special form of association that represents a whole-part relationship where the part can exist independently of the whole.
- In other words, a class can contain references to another class, but the contained object can still exist independently.
Example:
class Department {
String name;
List<Employee> employees;
Department(String name) {
this.name = name;
this.employees = new ArrayList<>();
}
}
class Employee {
String employeeName;
Employee(String employeeName) {
this.employeeName = employeeName;
}
}
- Here, employees can exist independently of the department, and a department may have employees, but employees can still exist without the department.
5. Composition
- Composition is a stronger association where the whole class is responsible for the lifetime of the part. The part cannot exist without the whole.
- If the whole object is destroyed, the part objects are destroyed as well.
Example:
class House {
Room room; // The room is part of the house, and cannot exist without it
House() {
this.room = new Room(); // Room is created when the house is created
}
}
class Room {
String roomType;
Room() {
this.roomType = "Bedroom";
}
}
- In this case, if a
House
is destroyed, theRoom
will be destroyed as well. TheRoom
object cannot exist without theHouse
.
6. Using Java Collections in Associations
Associations are often implemented using collections like List
, Set
, or Map
. These collections help in modeling complex relationships, such as one-to-many or many-to-many relationships.
- One-to-many association can be modeled using a
List
orSet
. - Many-to-many association can be modeled using a
Map
where keys and values both represent objects.
Conclusion
In summary, association in Java is about establishing relationships between classes, and it is a core concept for modeling real-world scenarios. Understanding the types of association helps in designing flexible and maintainable object-oriented systems.
#Java #OOP #ObjectOrientedProgramming #JavaAssociation #JavaInterview #InterviewPreparation #CodingInterview #SoftwareEngineering #JavaConcepts #JavaDeveloper #DataStructures #JavaProgramming