Saturday , August 19 2017
Home / java / OOPS / What is Association in Java

What is Association in Java

Object is a real world entity like 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 association and where it is used.

What is Association:

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

Hence you must know about the relation ships 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;
    }
 
}
[Rahul, Chandra] are Employees of Google
In the above example, one Organization has many number of Employees. There fore the relationship between the Organization and Employee is one to many. And also those two are two different entities. There fore the relationship between the two entities is called association.

Happy Learning 🙂

About chandrashekhar

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+ ,

Recommended

Java access modifiers

  Java access modifiers determines a particular class can access the data members or methods. …

Leave a Reply

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