Association in Java is a relationship between two separate classes that are independent of one another. It represents a Has-A relationship.
Association is one of the core concepts of object-oriented programming in Java. It defines how objects of one class communicate with objects of another class and use their functionalities or services.
In association, both classes have their own separate lifecycle, and there is no ownership between them. Therefore, one object can exist independently of the other.
Association establishes a multiplicity relation between objects where all objects have their own lifecycle and there is no owner of association (Has-A relationship).
Association establishes a multiplicity relationship between objects. Multiplicity specifies how many objects of one class can be associated with objects of another class.
Realtime Example of Association in Java
For example, consider the relationship between a Teacher and a Student:
- A single teacher can teach multiple students.
- A single student can learn from multiple teachers.
Here, there is no ownership between Teacher and Student objects. Both objects are independent and can exist separately.
- A teacher can exist without students.
- A student can exist without teachers.
Thus, both Teacher and Student have their own lifecycle and can be created or destroyed independently.
In UML (Unified Modeling Language), an association is represented by a solid line between two class objects that describe the relationship.
Types of Relationships Managed by Association in Java
Association in Java can manage different types of relationships between objects. These relationships define how objects are connected with each other.
The following are the main types of relationships managed by association in Java:
1. Unidirectional Association
When one object is associated with another object in only one direction, it is called a unidirectional association. It is also known as a one-way relationship. In this type of association, only one class can access or communicate with the other class.
For example, a department in college can have many students, but students may not know about the department object directly. This is an unidirectional in nature. Similarly, a tennis player has a racket. It is an unidirectional association because a racket does not have information about the tennis player.
2. Bidirectional Association
When two objects involved in association can communicate with each other in both directions, it is called bidirectional association. In this type of association, both classes maintain references to each other.
A common example of bidirectional relationship is “Person and Address”.
- A person can have associated with multiple addresses.
- An address can belong to multiple persons.
This represents a many-to-many bidirectional relationship.
Types of Multiplicity Relationships in Association
Association can establish the following multiplicity relationships:
- One-to-One
- One-to-Many
- Many-to-One
- Many-to-Many
Let us understand them with real-time examples.
1. One-to-One Relationship
In a one-to-one relationship, one object of a class is associated with only one object of another class. A common example of one-to-one relationship is:
- A person can have only one passport.
- A passport belongs to only one person.
2. One-to-Many Relationship
In a one-to-many relationship, one object of a class is associated with multiple objects of another class. A very good example of one-to-many relationship is “College and Students”.
- A college can have many students.
Another example of one-to-many relationship is “Bank and Employees”.
- A bank can have many working employees.
3. Many-to-One Relationship
In a many-to-one relationship, multiple objects of one class are associated with a single object of another class. An example of many-to-one relationship is “State and Cities”.
- A state can have many cities, but all the cities are related to that single state. Here, multiple city objects are associated with a single state object.
4. Many-to-Many Relationship
In a many-to-many relationship, multiple objects of one class are associated with multiple objects of another class. A common example of many-to-many relationship is “Teachers and Students”.
- A single student can associate with many teachers.
- Many students can also associate with a single teacher.
Since both Teacher and Student objects can exist independently, this is a many-to-many association relationship.
Examples of Association in Java
Let’s take example programs to implement all types of relationships.
Example 1: One-to-One Association
class Passport
{
private int passportNo;
public int getPassportNo() {
return passportNo;
}
public void setPassportNo(int passportNo) {
this.passportNo = passportNo;
}
}
class Person
{
private String name;
// Association with Passport class
private Passport passport;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Passport getPassport() {
return passport;
}
public void setPassport(Passport passport) {
this.passport = passport;
}
}
public class OneToOneTest
{
public static void main(String[] args)
{
Passport pp1 = new Passport();
pp1.setPassportNo(1234567);
Person p1 = new Person();
p1.setName("John");
// Establishing one-to-one association.
p1.setPassport(pp1);
System.out.println(
p1.getName() +
" has passport number: " +
p1.getPassport().getPassportNo()
);
}
}Output:
John has passport number: 1234567
In this example program, the association between Person and Passport is accomplished in the main method and shows one-to-one relationship.
Example 2: One-to-Many Association
class Student
{
private String name;
public Student(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
class College
{
private String collegeName;
// One college has many students.
private Student[] students;
public College(String collegeName, Student[] students)
{
this.collegeName = collegeName;
this.students = students;
}
public void display()
{
System.out.println("College Name: " + collegeName);
System.out.println("Students of the college are:");
for(Student s : students)
{
System.out.println(s.getName());
}
}
}
public class OneToManyTest
{
public static void main(String[] args)
{
// Creating Student objects
Student s1 = new Student("Rahul");
Student s2 = new Student("Aman");
Student s3 = new Student("Priya");
// Storing student objects in array.
Student[] students = {s1, s2, s3};
// Creating College object.
College c = new College("ABC College", students);
// Displaying data
c.display();
}
}Output:
College Name: ABC College Students of the college are: Rahul Aman Priya
In this example program:
- Student is a separate class.
- College class contains multiple Student objects using an array.
- The line private Student[] students; creates the association between College and many Student objects.
Thus, one college associated with many students represents a one-to-many relationship in Java.
Example 3: Many-to-One Association
class State
{
private String stateName;
public State(String stateName)
{
this.stateName = stateName;
}
public String getStateName()
{
return stateName;
}
}
class City
{
private String cityName;
// Many cities belong to one state.
private State state;
public City(String cityName, State state)
{
this.cityName = cityName;
this.state = state;
}
public void display()
{
System.out.println(cityName + " is in " + state.getStateName());
}
}
public class ManyToOneTest
{
public static void main(String[] args)
{
// Creating one State object
State s = new State("Jharkhand");
// Creating multiple City objects.
City c1 = new City("Dhanbad", s);
City c2 = new City("Ranchi", s);
City c3 = new City("Jamshedpur", s);
// Displaying information.
c1.display();
c2.display();
c3.display();
}
}Output:
Dhanbad is in Jharkhand Ranchi is in Jharkhand Jamshedpur is in Jharkhand
In this example program:
- State class represents one state.
- City class contains a reference to the State object.
- The line private State state; establishes the association between many City objects and one State object.
Thus, many cities associated with one state represents a many-to-one relationship in Java.
Example 4: Many-to-Many Association
class Address
{
private String city;
private String state;
public Address(String city, String state)
{
this.city = city;
this.state = state;
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
}
class Person
{
private String name;
// One person can have multiple addresses.
private Address[] addresses;
public Person(String name, Address[] addresses)
{
this.name = name;
this.addresses = addresses;
}
public void display()
{
System.out.println("Person Name: " + name);
System.out.println("Addresses:");
for(Address a : addresses)
{
System.out.println(a.getCity() + ", " + a.getState());
}
System.out.println();
}
}
public class ManyToManyTest
{
public static void main(String[] args)
{
// Creating Address objects
Address a1 = new Address("Dhanbad", "Jharkhand");
Address a2 = new Address("Mumbai", "Maharashtra");
// Same addresses can belong to multiple persons
Address[] addr1 = {a1, a2};
Address[] addr2 = {a1, a2};
// Creating Person objects
Person p1 = new Person("John", addr1);
Person p2 = new Person("Shubh", addr2);
// Displaying information
p1.display();
p2.display();
}
}Output:
Person Name: John Addresses: Dhanbad, Jharkhand Mumbai, Maharashtra Person Name: Shubh Addresses: Dhanbad, Jharkhand Mumbai, Maharashtra
In this example program:
- One person has multiple addresses.
- The same address objects can also belong to multiple persons.
This creates a proper many-to-many association in Java.
Types of Association in Java
There are two combinations of association possible in Java:
- Aggregation
- Composition
Both represent Has-A relationships.
a) Aggregation
Aggregation in Java is a special form of unidirectional association that represents a weak Has-A relationship between two objects.
In aggregation:
- Both objects have their own independent lifecycle.
- One object contains a reference to another object.
- The contained object can exist independently even if the container object is destroyed.
Aggregation is also called a whole-part relationship.
A common example of aggregation is the relationship between a Car and Passengers.
- A car can have passengers.
- Passengers can exist independently without the car.
- Even if the car is destroyed, passengers can still exist.
Therefore, this relationship represents aggregation in Java. You will learn in more detail about aggregation with example programs in the next tutorial.
b) Composition
A composition is a special and more restrictive form of aggregation that represents a strong Has-A relationship.
In composition:
- One object completely owns another object.
- The child object cannot exist independently without the parent object.
- If the parent object is destroyed, the child object is also destroyed.
Composition represents a strong dependency between objects.
A common example of composition is the relationship between a Car and its Engine.
- A car contains an engine.
- The engine is strongly dependent on the car in this relationship.
- If the car is destroyed, the engine is also considered destroyed as part of that car object.
Therefore, this relationship represents composition.
To get more details, read this: Composition in Java
Benefits of Association in Java
There are several advantages of using association in Java programming:
- Classes remain separate and manageable, enhancing code structure. The code becomes more modular and easier to understand.
- We can reuse the classes in different contexts by establishing associations. It reduces redundancy and enhances code reusability.
- Association provides flexibility, as we can accommodate modifications in one class without affecting the entire system.
- Interaction between classes is controlled, improving security.
Best Practices for Utilizing Association
There are the following key points that you should keep in mind when you implement an association between classes.
- Clearly define the purpose of the association between classes so that the relationship is meaningful and aligns with the overall design..
- Use meaningful class and method names that reflect the nature of the association. This makes the code more readable and understandable.
- Avoid excessive coupling between associated classes.
- Understand the cardinality of the relationship (one-to-one, one-to-many, many-to-many).
- Do not try to overcomplicate relationships between classes.
- Use unit testing to verify the behavior of associated classes.
- Debug by tracing the flow of data and interactions between associated classes.






