Introduction to Association in Java
Association in Java is a relation or connection between two classes set up through their Objects. The association represents that a class knows about another class and holds a reference to another class; it can be described as a “has-a” relationship because it implements in Java through the use of an instance field. The association relationship can be bi-directional, as each class has a reference to the other. Does association relationship represent how objects connect with each other? How do they communicate with each other? And how do they use each other’s functionality? An association relation can be four types: one-to-one, many-to-one, one-to-many and many-to-many.
Forms of Association in Java
The two forms of association are Aggregation and Composition.
1. Aggregation
Aggregation is a relation between two classes set up through entity reference; it can be described as a has-a and part or whole relationship. An aggregation is a form of association that is a one-way or unidirectional association. For example, customers can have orders, but the reverse is impossible, hence unidirectional.
class Customer{
int cid;
String cname;
Order ordering;
}
2. Composition
Composition relation is a restricted form of Aggregation in which two classes (or entities) are highly dependent on each other; the composed object cannot exist without the other entity. The composition can be described as a part-of relationship.
Examples of Association in Java
Following are the different examples of association in Java.
Example #1 – Association Relation
Here, we write the Java code to understand the Association relation more clearly with the following example.
Code:
package demo;
import java.io.*;
class Department
{
private String dname;
public void setDepartmentName(String cname)
{
this.dname = cname;
}
public String getDepartmentName()
{
return this.dname;
}
}
class Emp
{
public String ename;
public void setEmployeeName(String ename)
{
this.ename = ename;
}
public String getEmployeeName()
{
return this.ename;
}
}
class demo
{
public static void main (String[] args)
{
Department d = new Department();
Department d1 = new Department();
Emp e = new Emp();
Emp e1 = new Emp();
d.setDepartmentName("Sales");
d1.setDepartmentName("Accounting");
e.setEmployeeName("John");
e1.setEmployeeName("Joseph");
System.out.println(e.getEmployeeName() + " is working in " + d.getDepartmentName() + ".");
System.out.println(e1.getEmployeeName() + " is working in " + d1.getDepartmentName() + ".");
}
}
Output:
In the above code, two Classes, Company and Employee, through their Objects, represent a one-to-many relationship as the class Company can have many employees.
Example #2 – Aggregation Relation
Next, we write the Javacode to understand the aggregation relation more clearly with the following example.
Code:
import java.io.*;
import java.util.*;
class Customer
{
String cname;
int cid ;
String type;
public static int cpp=0,ccod=0;
Customer(String cname, int cid, String type)
{
this.cname = cname;
this.cid = cid;
this.type = type;
if(type=="prepaid")
cpp++;
else
ccod++;
}
}
class Order
{
String type;
//list of customer Objects associated with customer class as with its Object(s). */
private List<Customer> customers;
public void setCustomers(String type, List<Customer> customers)
{
this.type = type;
this.customers = customers;
}
public List<Customer> getCustomers()
{
return customers;
}
}
class OnlineStore
{
// list of order Objects which associated with order as with its objects.
private List<Order> orders;
public void setnoCustomer( List<Order> orders)
{
this.orders = orders;
}
// count total customers of all orders in a given purchases
public int getnoCustomer()
{
int noOfOrders = 0;
List<Customer> customers;
for(Order dept : orders)
{
customers = dept.getCustomers();
for(Customer s : customers)
{
noOfOrders++;
}
}
return noOfOrders;
}
}
class demo
{
public static void main (String[] args)
{
Customer c1 = new Customer("Mia", 1, "prepaid");
Customer c2 = new Customer("Priya", 2, "prepaid");
Customer c3 = new Customer("John", 1, "COD");
Customer c4 = new Customer("Rahul", 2, "COD");
List <Customer> cu1 = new ArrayList<Customer>();
cu1.add(c1);
cu1.add(c2);
// making a List of COD Customers
List <Customer> cu2 = new ArrayList<Customer>();
cu2.add(c3);
cu2.add(c4);
Order pp = new Order();
pp.setCustomers("prepaid", cu1);
Order cod = new Order();
cod.setCustomers("COD", cu2);
List <Order> orders = new ArrayList<Order>();
orders.add(pp);
orders.add(cod);
// creating an OnlineStore instance.
OnlineStore purchase = new OnlineStore();
purchase.setnoCustomer(orders);
System.out.print("Number of Customers placed order are : ");
System.out.println(purchase.getnoCustomer());
System.out.print("Number of Customers placed prepaid order are : ");
System.out.println(Customer.cpp);
System.out.print("Number of Customers placed cod order are : ");
System.out.println(Customer.cpp);
}
}
Output:
As in the above code, an online store has no. of orders like prepaid and COD. Every type of order has no. of customers, an OnlineStore class concerning objects, or no. of Objects of the Order class. The onlineStore class is associated with the Order class through its Objects and Order class. It also refers to the object (s) of the Customer class, which means it is related to the Customer class through its Objects, representing a Has-A relationship.
Example #3 – Composition Relation
Next, we write the Java code to understand the Composition relation more clearly with the following example.
Code:
class Car
{
private String color;
private int wheels;
public void carFeatures()
{
System.out.println("The color of the car is "+color + " and number of wheels are " + wheels);
}
public void setColor(String color)
{
this.color = color;
}
public void setwheels(int wheels)
{
this.wheels = wheels;
}
}
class Maruti extends Car
{
public void setStart()
{
MarutiEngine e = new MarutiEngine();
e.run();
}
}
class MarutiEngine extends Maruti
{
public void run()
{
System.out.println("Engine is running...");
}
public void stop()
{
System.out.println("Engine is not running... ");
}
}
public class demo
{
public static void main(String[] args)
{
Maruti m1 = new Maruti();
m1.setwheels(4);
m1.setColor("White");
m1.carFeatures();
m1.setStart();
}
}
Output:
As in the above code, a Car is the superclass of the Maruti class, which means the Car can have no. of Maruti cars, and Maruti uses MarutiEngine as its part; if Maruti does not exist, then MarutiEngine also will not exit. Thus Maruti and MarutiEngine class describe composition relation.
Use of Association in Java:
As we have seen above, the need for an association is for code reusability. For example, we have used the two classes above, Customer class, Order class, and OnlineStore class maintain customer information, and programmers do not need to use the same code again and again.
Conclusion
In Java, you establish an association, which is a relation between two classes, by setting up their objects. The forms of an association are Aggregation and composition. The primary purpose of association in Java is for code reusability.
Recommended Articles
This is a guide to the Association in Java. Here we discuss the Introduction, the two forms of Association in Java, and examples and code implementation. You may also look at the following articles to learn more –