OOP Concepts

What is Association in Java

An object is a real-world entity like a pen, table, book and many more. Here an Object-Oriented Programming is a paradigm or methodology for designing the applications using the objects and classes.

OOPS simplifies all the software developments using the OOPs Principles. In this tutorials, we are going to understand what is an association and where it is used.

What is Association:

Association establishes a relationship between any two objects. It can be defined as the multiplicity between the objects.

Hence you must know about the relationships between two objects, like one-to-one, many-to-one, one-to-many and many-to-many as all these are the association between objects.

Association also has its special form with Aggregation and Composition is the special form of aggregation.

For Example, Organization and Employee have an association.

Association

[java]

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

public class AssociationDemo {
 
    public static void main(String[] args) {
        Organization organization = new Organization();
        organization.setOrganizationName("Google");
        
        
        Employee employee = new Employee();
        employee.setEmployeeName("Rahul");
        Employee employee2 = new Employee();
        employee2.setEmployeeName("Chandra");
        
        List<Employee> empList = new ArrayList();
        empList.add(employee);
        empList.add(employee2);
        
        
        organization.setEmployees(empList);
        
        
        System.out.println(organization.getEmployees()+" are Employees of "+
                organization.getOrganizationName());
    }
 
}
 
class Organization {
 
    private String organizationName;
    
    List<Employee> employees;
 
    public String getOrganizationName() {
        return organizationName;
    }
 
    public void setOrganizationName(String organizationName) {
        this.organizationName = organizationName;
    }

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

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
 
}
 
class Employee {
 
    private String employeeName;
 
    public String getEmployeeName() {
        return employeeName;
    }
 
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    @Override
    public String toString() {
        return employeeName;
    }
 
}
[/java]

Output:

[Rahul, Chandra] are Employees of Google

In the above example, one Organization has many numbers of Employees. Therefore the relationship between the Organization and Employee is one-many. And also those two are two different entities. Therefore the relationship between the two entities is called association.

Happy Learning :)

Found a mistake or something out of date? Tell us →

Written by

chandrashekhar

Founder of onlinetutorialspoint.com. Enterprise architect with 15+ years in healthcare technology, designing and running systems on AWS and Azure. Writes about building real things with AI, local AI hardware and cloud certifications.

Keep reading

1 comment

Ask a question or leave a comment