Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Association in Java?
3.
Example of Association in Java
3.1.
Java
3.2.
Java
4.
Types of Association in Java
4.1.
One-to-One Association
4.2.
Java
4.3.
One-to-Many Association
4.4.
Many-to-Many Association
4.5.
Java
5.
Frequently Asked Questions
5.1.
What is the main advantage of using association in Java?
5.2.
What is the association between two classes in Java?
5.3.
What is the difference between class joins and association?
5.4.
Can an association be bidirectional in Java?
5.5.
How do you choose between different types of associations?
6.
Conclusion
Last Updated: Sep 10, 2024
Medium

Association in Java

Author Gaurav Gandhi
0 upvote

Introduction

In Java, association is a relationship between two classes where one class uses the functionalities or services provided by the other class. It is a way to establish a connection between objects of different classes. Association helps in creating a structure where objects can interact with each other to achieve a specific functionality. 

Association in Java

In this article, we will learn the concept of association in Java, and understand its types, with proper examples.

What is Association in Java?

In Java, association is a relationship between two classes that allows one object to interact with another. It defines how objects from different classes work together while maintaining their independence. Association can be one-to-one, one-to-many, many-to-one, or many-to-many, and it represents a general connection without ownership between objects.

Example of Association in Java

To illustrate association in Java, let’s consider a simple scenario involving two classes: Book and Author. In this relationship, an Author can write multiple Books, but each Book is written by only one Author. This example demonstrates a one-to-many association.

Here’s how you can implement this in Java:

  • Java

Java

class Author {
   String name;
   List<Book> books;
   Author(String name) {
       this.name = name;
       this.books = new ArrayList<>();
   }
   void addBook(Book book) {
       books.add(book);
   }
}

class Book {
   String title;
   Author author;

   Book(String title, Author author) {
       this.title = title;
       this.author = author;
   }
}
You can also try this code with Online Java Compiler
Run Code


In this code:

  • The Author class has a name attribute to store the author’s name & a list of Book objects to represent the books written by the author.
     
  • The Book class has a title attribute for the book title & an author attribute linking back to the Author who wrote the book.


This setup allows us to manage books associated with their respective authors effectively. For example, to create an author and assign books to them, you could do the following:

  • Java

Java

public class Main {
   public static void main(String[] args) {
       Author author = new Author("J.K. Rowling");
       Book book1 = new Book("Harry Potter and the Sorcerer's Stone", author);
       Book book2 = new Book("Harry Potter and the Chamber of Secrets", author);
      
       author.addBook(book1);
       author.addBook(book2);
      
       System.out.println("Books written by " + author.name + ":");
       for(Book book : author.books) {
           System.out.println(book.title);
       }
   }
}
You can also try this code with Online Java Compiler
Run Code


This code creates an Author object for J.K. Rowling and two Book objects. Each book is added to the list of books written by the author, demonstrating their association. When you run this program, it will display the titles of the books written by J.K. Rowling.

Types of Association in Java

Association in Java can be broadly classified into three types based on how classes are related and interact with each other: 

  1. one-to-one, 
     
  2. one-to-many, and 
     
  3. many-to-many. 

Each type has its specific use cases and implementation methods.

One-to-One Association

In a one-to-one association, each instance of one class is associated with one instance of another class. For example, consider a scenario where each person has a unique driver's license. Here, a Person class and a License class would have a one-to-one relationship.

  • Java

Java

class Person {
   String name;
   License license; // each person has a unique license

   Person(String name, License license) {
       this.name = name;
       this.license = license;
   }
}

class License {
   String licenseNumber;
   Person person; // each license is assigned to a specific person

   License(String licenseNumber, Person person) {
       this.licenseNumber = licenseNumber;
       this.person = person;
   }
}
You can also try this code with Online Java Compiler
Run Code

One-to-Many Association

A one-to-many association occurs when an instance of one class can be associated with multiple instances of another class. This was demonstrated in the earlier example where an Author can have multiple Books.

Many-to-Many Association

In a many-to-many association, multiple instances of one class can be associated with multiple instances of another class. For example, a Student class and a Course class where a student can enroll in multiple courses and each course can have multiple students.

  • Java

Java

class Student {
   String name;
   List<Course> courses; // students can enroll in multiple courses

   Student(String name) {
       this.name = name;
       this.courses = new ArrayList<>();
   }
   void enroll(Course course) {
       courses.add(course);
   }
}

class Course {
   String courseName;
   List<Student> students; // a course can have multiple students

   Course(String courseName) {
       this.courseName = courseName;
       this.students = new ArrayList<>();
   }

   void addStudent(Student student) {
       students.add(student);
   }
}
You can also try this code with Online Java Compiler
Run Code

Frequently Asked Questions

What is the main advantage of using association in Java?

The main advantage of using association in Java is that it allows for flexible code where objects can remain independent yet collaborate. This increases reusability and modularity of the code.

What is the association between two classes in Java?

In Java, association refers to a relationship where two classes are connected, allowing one object to communicate with another. This relationship is without ownership, meaning both classes can function independently while interacting with each other.

What is the difference between class joins and association?

Class joins typically refer to combining data from two or more classes in databases, while association in Java describes a relationship between classes that allows objects to interact without being dependent on one another. They differ in context and purpose.

Can an association be bidirectional in Java?

Yes, an association can be bidirectional, where both classes involved are aware of each other and can interact mutually. For example, in a many-to-many relationship between Student and Course, both classes can maintain a list of each other.

How do you choose between different types of associations?

The choice depends on the specific requirements of the application. One-to-one is used when objects directly correspond to each other, one-to-many for a single source managing multiple items, and many-to-many when the interaction is more complex involving numerous objects on both sides.

Conclusion

In this article, we have learned about the concept of association in Java and its practical applications through various types: one-to-one, one-to-many, and many-to-many. Understanding these relationships is crucial for designing effective object-oriented programs that are modular, maintainable, and scalable. We have also cleared every doubt with the relevant examples.

You can refer to our guided paths on Code360 You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass