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
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
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:
- one-to-one,
- one-to-many, and
- 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
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
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 DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.