Association In Java

we will discuss Association in Java it’s very simple Association establishes relationship between two separate classes through their objects.In object-oriented programming, an object communicates to another object to use functionality and services provided by that object.

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.

Aggregation is a special form of association which is a unidirectional one way relationship between classes (or entities), for e.g. Wallet and Money classes. Wallet has Money but money doesn’t need to have Wallet necessarily so its a one directional relationship. In this relationship both the entries can survive if other one ends. In our example if Wallet class is not present, it does not mean that the Money class cannot exist.

Composition is a restricted form of Aggregation in which two entities (or you can say classes) are highly dependent on each other. For e.g. Human and Heart. A human needs heart to live and a heart needs a Human body to survive. In other words when the classes (entities) are dependent on each other and their life span are same (if one dies then another one too) then its a composition. Heart class has no sense if Human class is not present.

For Example, Organization and Employee have an association.


import java.util.ArrayList;

import java.util.List;

public class AssociationDemo {

public static void main(String[] args) {

Organization organization = new Organization();


Employee emp = new Employee();

Employee emp2 = new Employee();

Employee emp3 = new Employee();

List<Employee> empList = new ArrayList();



System.out.println(organization.getEmployees()+" are Employees of "+




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;
public String toString() {
return employeeName;
[Kavitha,Sekhar,Haritha] are Employees of VBBS.



Let’s take another example of the relationship between Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers. But there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.
 public class Teacher {
   private String name;
   private List<Student> students;
   // getter and setter methods 
public class Student {
   private String name;
   private List<Teacher> teachers;
   // getter and setter methods 


Leave a Reply

Your email address will not be published. Required fields are marked *