What is Association in Java

By | 2018-05-09T02:29:30+00:00 April 7th, 2015|OOPS|0 Comments

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


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;
    }
 
}

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 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment