Introduction to Association in Java
Association in Java is a relation or connection between two classes that 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 having a reference to the other. Does association relationship represent how objects connect with each other? how they communicate with each other? and how they use each other’s functionality? An association relation can be four types, which are 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 which setup through entity reference; it can be described as has-a and part or whole relationship. An aggregation is a form of Association, which is a one-way relationship or a unidirectional association. For example, customers can have orders, but the reverse is not possible, hence unidirectional in nature.
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 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 which has a reference to Object or no. of Objects of the Order class. OnlineStore class is associated with Order class through its Objects and Order class. It also has a reference to Object(s) of Customer class, which means it is associated with 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 Car can have no. of Maruti cars, and Maruti uses MarutiEngine as its part; if Maruti does not exit, then MarutiEngine also will not exits. 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, as we have used two classes above, Customer class, Order class, and OnlineStore class, maintaining customer information and programmers does not need to use the same code again and again.
Conclusion
In Java, the association is a relation between two classes that set up through their Objects. The forms of an association are aggregation and composition. The main 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 and the two forms of Association in Java and examples and code implementation. You may also look at the following articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses