Association in Java with Example

Introduction

Association is a relationship between two classes that establishes a connection between their objects. It defines how objects of one class are connected to objects of another class. Association can be of different types: unidirectional, bidirectional, one-to-one, one-to-many, many-to-one, and many-to-many. Understanding association is essential for modeling relationships in object-oriented design.

Table of Contents

  1. What is Association?
  2. Types of Association
  3. Benefits of Association
  4. Example: Unidirectional Association
  5. Example: Bidirectional Association
  6. Real-World Examples of Association
  7. Conclusion

1. What is Association?

Association is a structural relationship that represents how objects of one class are related to objects of another class. Unlike inheritance, which defines an is-a relationship, association defines a has-a relationship. It indicates that one object uses or interacts with another object.

2. Types of Association

  • Unidirectional Association: One class knows about the other class, but not vice versa.
  • Bidirectional Association: Both classes know about each other.
  • One-to-One Association: One object of a class is associated with one object of another class.
  • One-to-Many Association: One object of a class is associated with many objects of another class.
  • Many-to-One Association: Many objects of a class are associated with one object of another class.
  • Many-to-Many Association: Many objects of a class are associated with many objects of another class.

3. Benefits of Association

  • Reusability: Promotes code reuse by establishing relationships between classes.
  • Flexibility: Allows classes to interact and collaborate without being tightly coupled.
  • Modularity: Helps in creating modular designs by defining clear relationships between classes.
  • Improved Design: Facilitates better design by modeling real-world relationships.

4. Example: Unidirectional Association

In a unidirectional association, one class knows about the other class, but not vice versa.

Example:

Let's create a Library class that is associated with multiple Book objects using unidirectional association.

Step 1: Define the Book Class

public class Book {
    private String title;
    private String author;

    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }
}

Step 2: Define the Library Class

import java.util.ArrayList;
import java.util.List;

public class Library {
    private List<Book> books;

    public Library() {
        this.books = new ArrayList<>();
    }

    public void addBook(Book book) {
        books.add(book);
    }

    public void showBooks() {
        for (Book book : books) {
            System.out.println("Title: " + book.getTitle() + ", Author: " + book.getAuthor());
        }
    }
}

Step 3: Main Class to Demonstrate Unidirectional Association

public class Main {
    public static void main(String[] args) {
        Book book1 = new Book("1984", "George Orwell");
        Book book2 = new Book("To Kill a Mockingbird", "Harper Lee");

        Library library = new Library();
        library.addBook(book1);
        library.addBook(book2);

        library.showBooks();
        // Output:
        // Title: 1984, Author: George Orwell
        // Title: To Kill a Mockingbird, Author: Harper Lee
    }
}

Explanation:

  • Book: A simple class with title and author attributes.
  • Library: A class that uses unidirectional association to include multiple Book objects. The Library class can add and display books.
  • Main: A class to demonstrate the use of unidirectional association by creating Book objects and adding them to the Library.

5. Example: Bidirectional Association

In a bidirectional association, both classes know about each other.

Example:

Let's create a Person class and an Address class with a bidirectional association.

Step 1: Define the Address Class

public class Address {
    private String street;
    private String city;
    private Person person; // Bidirectional association

    public Address(String street, String city) {
        this.street = street;
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public String getCity() {
        return city;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Person getPerson() {
        return person;
    }
}

Step 2: Define the Person Class

public class Person {
    private String name;
    private Address address; // Bidirectional association

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAddress(Address address) {
        this.address = address;
        address.setPerson(this); // Set the reverse association
    }

    public Address getAddress() {
        return address;
    }
}

Step 3: Main Class to Demonstrate Bidirectional Association

public class Main {
    public static void main(String[] args) {
        Person person = new Person("John Doe");
        Address address = new Address("123 Main St", "Springfield");

        person.setAddress(address);

        System.out.println("Person: " + person.getName());
        System.out.println("Address: " + person.getAddress().getStreet() + ", " + person.getAddress().getCity());
        System.out.println("Resident: " + address.getPerson().getName());
        // Output:
        // Person: John Doe
        // Address: 123 Main St, Springfield
        // Resident: John Doe
    }
}

Explanation:

  • Address: A class with street, city, and a reference to a Person object.
  • Person: A class with name and a reference to an Address object.
  • Main: A class to demonstrate bidirectional association by creating Person and Address objects and setting up their association.

6. Real-World Examples of Association

Example 1: University and Student

In a university system, a University class can have multiple Student objects. This can be modeled using unidirectional or bidirectional association.

University Class

import java.util.ArrayList;
import java.util.List;

public class University {
    private String name;
    private List<Student> students;

    public University(String name) {
        this.name = name;
        this.students = new ArrayList<>();
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    public List<Student> getStudents() {
        return students;
    }

    public String getName() {
        return name;
    }
}

Student Class

public class Student {
    private String name;
    private University university; // Bidirectional association

    public Student(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setUniversity(University university) {
        this.university = university;
    }

    public University getUniversity() {
        return university;
    }
}

Example 2: Department and Employee

In a company, a Department can have multiple Employee objects, and an Employee can belong to a single Department.

Department Class

import java.util.ArrayList;
import java.util.List;

public class Department {
    private String name;
    private List<Employee> employees;

    public Department(String name) {
        this.name = name;
        this.employees = new ArrayList<>();
    }

    public void addEmployee(Employee employee) {
        employees.add(employee);
    }

    public List<Employee> getEmployees() {
        return employees;
    }

    public String getName() {
        return name;
    }
}

Employee Class

public class Employee {
    private String name;
    private Department department; // Bidirectional association

    public Employee(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public Department getDepartment() {
        return department;
    }
}

7. Conclusion

Association in Java is a powerful concept that allows modeling relationships between classes. By understanding and using association correctly, developers can create flexible, modular, and maintainable systems. Association can be unidirectional or bidirectional and can represent one-to-one, one-to-many, many-to-one, or many-to-many relationships.

Happy coding!

Comments

  1. Hi where is the complete working example of association in java ?
    Do update this article !!!

    ReplyDelete

Post a Comment

Leave Comment