Updated April 5, 2023
Introduction to Association in OOPs
Association in OOPs is a relationship between two separate classes, which is established with the help of objects. As we know, in OOPs (Object Oriented Programming), objects communicate with each other to use each other’s functionality and services. In Association, relationships between the classes can be one-to-one, one-to-many, many-to-many. It refers to the “using” relationship between the objects. Each object in Associate has its own lifetime and has no owner.
Let us consider an example of a Teacher and Student, One teacher can have multiple students, and one student can have multiple teachers for separate subjects. Each teacher and student changes (can be added and removed from the college) on time. In short, they have their own lifetime.
Forms of Association
There are basically 2 forms of Associations:
1. Aggregation
Aggregation is a special type of Association which follows a “has-a” relationship between 2 related objects. In Aggregation, there is a one-way relationship. Both the entities in Association are independent of each other, which means if one entity is deleted or fails due to some issue, it will not affect the other one. In Aggregation, entities are loosely connected with each other or, in other words, it has a weak association; they are independent of each other.
For example, a Teacher can have one Course to teach. If the teacher is removed or moved out of the college, there is no effect on the Course and vice versa.
2. Composition
Composition is a restricted form of Association, but the entities are strongly dependent on each other, unlike Aggregation. It has a strong association, which means if the parent entity is destroyed, there is no meaning of the child entity’s existence. It represents the “part-of” relationship.
For example, there are many books in a library, but if the Library of the college is destroyed, then there is no use for the books; the books are also destroyed.
Let us see the Association with the help of some theoretical examples:
- One Person can have many Cars (one-to-many relationships).
- Many Students can be present in one Department, but not 2 or more Departments can have one Student. (many-to-one relationship).
- One Person can have one legal Aadhar Card (one-to-one relationship)
- Customers can purchase various Products, and Many Customers can purchase one Product. (many-to-many relationships).
How does Association Work in OOPs along with Examples
Let us see how the Association works in Java code which follows the OOPs concept along with the examples:
Example #1: Aggregation Program
Code:
import java.util.*;
// Employee class having the Employee information
class Employee
{
String name;
int emp_id ;
String dept;
Employee(String name, int emp_id, String dept)
{
this.name = name;
this.emp_id = emp_id;
this.dept = dept;
System.out.println("Employee name is "+ name);
System.out.println("Employee id is" + emp_id);
System.out.println("Employee department is "+ dept);
}
}
// Department class having the department name and list of Employees working in various departments
class Department
{
String dept_name;
private List<Employee> emps;
Department(String name, List<Employee> emps)
{
this.dept_name = dept_name;
this.emps = emps;
}
public List<Employee> EmployeeRecord()
{
return emps;
}
}
// main method to drive the code
class Main
{
public static void main (String[] args)
{
Employee e1 = new Employee("Abc", 1001, "Development");
Employee e2 = new Employee("Xyz", 1002, "Testing");
Employee e3 = new Employee("Tuv", 1003, "HR");
// Constructing list of Development Department employees.
List <Employee> dev = new ArrayList<Employee>();
dev.add(e1);
//Constructing the list of Testing department employees.
List <Employee> test = new ArrayList<Employee>();
test.add(e2);
//Constructing the list of HR department Employees.
List <Employee> hr = new ArrayList<Employee>();
hr.add(e3);
}
}
Output:
Explanation:
- In the above example, an employee works in a Department. It follows the “Has -a”, which is a weak relationship. Employee class is defined with its constructor having the properties of the Employees like name, id and the department. Every Department has several Employees working in it.
- There is a Department class with the department name (dept_name) and the list of employees. Then, in the main function, Employee objects are created by passing all the values, and the specific employees are added in the particular department. Finally, respective details of the Employee are printed on the console using the System.out.println().
Example #2: Composition Program
Code:
import java.util.*;
//class Book with the properties name, author and book type
class Book
{
public String name;
public String author;
public String type;
Book(String name, String author, String type)
{
this.name = name;
this.author = author;
this.type = type;
}
}
//class College_Library having all the Books
class College_Library
{
private final List<Book> bks;
College_Library (List<Book> bks)
{
this.bks = bks;
}
public List<Book> totalBooks(){
return bks;
}
}
//Main function to drive the code
public class Main {
public static void main (String[] args)
{
Book b1 = new Book("A Passage to India.","XYZ", "Literature");
Book b2 = new Book("Beloved", " ABC", "Fiction");
Book b3 = new Book("the Making of the Modern World", "YUC", "History");
//creating the List of all the books
List<Book> bks = new ArrayList<Book>();
bks.add(b1);
bks.add(b2);
bks.add(b3);
College_Library lib = new College_Library(bks);
List<Book> books = lib.totalBooks();
for(Book bb : books){
System.out.println("Name of the book is : " + bb.name + " Book Author is : " + bb.author + " Book Type is : "+
bb.type);
}
}
}
Output:
Explanation:
- In the above code, the relationship between the Book and the College Library is strong relationship. If the College Library does not exist, there is no existence of the Books present in it. Book class is created with all the attributes of the book like name, author and its type. College_Library class is created with the list of the books present in it.
- In the main function, objects of the Book b1, b2 and b3 are created by passing the values of their respective attributes. Next, books are added in the ArrayList using the add() method. Then, using the simple loops of Java, details of the books are printed on the console.
Conclusion
The above description clearly explains what the Association in OOPs is and how it works and is practically implemented in any OOPs programming language like Java code. Association is one of the most important OOPs concepts and is widely used in the programs to create the relationship between the classes with the help of objects. Therefore, it is very important for a programmer to understand the forms of Association, i.e. Aggregation and Composition and the difference between the two.
Recommended Articles
We hope that this EDUCBA information on “Association in OOPs” was beneficial to you. You can view EDUCBA’s recommended articles for more information.