What is Association in Java?
How do you establish a connection between two classes when you are writing a Java Program? It’s simple. You can make use of a concept called association. Sounds interesting right? In this article, let’s check out the Association in Java in detail.
The topics discussed in this article are:
- What is the Association in Java?
- Two forms of Association
- Aggregation
- Composition
What is the Association?
Association in Java is a connection or relation between two separate classes that are set up through their objects. Association relationship indicates how objects know each other and how they are using each other’s functionality. It can be one-to-one, one-to-many, many-to-one, and many-to-many.
- For example, a person can have only one passport. That is a “one-to-one” relationship.
- If we talk about the association between a Bank and Employee, a bank can have many employees, So it is a “one-to-many” relationship.
- Similarly, every city exists in exactly one state, but a state can have many cities, which is a “many-to-one” relationship.
- Lastly, if we talk about the association between a teacher and a student, multiple students can be associated with a single teacher and a single student can also be associated with multiple teachers but both can be created or deleted independently. This is a “many-to-many” relationship.
Let’s understand about Association with an example.
package MyPackage;
import java.util.*;class CityClass {
private String cityName;
public String getCityName() {
return cityName;
}public void setCityName(String cityName) {
this.cityName = cityName;
}
@Override
public String toString() {
return cityName;
}
}class State {
private String stateName;
List<CityClass> citys;public String getStateName() {
return stateName;
}public void setStateName(String stateName) {
this.stateName = stateName;
}
public List<CityClass> getCities() {
return citys;
}
public void setState(List<CityClass> citys) {
this.citys = citys;
}
}
public class AssociationExample {
public static void main(String[] args) {
State state = new State();
state.setStateName("California");CityClass city = new CityClass();
city.setCityName("Los Angeles");
CityClass city2 = new CityClass();
city2.setCityName("San Diago");List<CityClass> empList = new ArrayList<CityClass>();
empList.add(city);
empList.add(city2);state.setState(empList);System.out.println(state.getCities()+" are cities in the state "+
state.getStateName());
}
}
Output:
[Los Angeles, San Diago] are cities in the state of California
As you can see, in this example program there are two classes, namely, states, and These two separate classes are associated through their Objects. Moreover, every city exists in exactly one state, but a state can have many cities, hence the term “many-to-one” relationship. Importantly, the association in Java has two special forms. Let’s check them out.
Two Forms of Association
Composition and Aggregation are the two special forms of association. Let’s check them out with the help of an example.
Composition
It is a “belongs-to” type of association. It simply means that one of the objects is a logically larger structure, which contains the other object. In other words, it’s part or member of the larger object. Alternatively, it is often called a “has-a” relationship (as opposed to an “is-a” relationship, which is inheritance).
For example, a building has a room, or in other words, a room belongs to a building. The composition is a strong kind of “has-a” relationship because the objects’ lifecycles are tied. It means that if we destroy the owner object, its members also will be destroyed with it. For example, if the building is destroyed the room is destroyed as well in our previous example. But, note that doesn’t mean, that the containing object can’t exist without any of its parts. For example, if we tear down all the rooms inside a building, the building will still exist.
Aggregation
Aggregation is also a “has-a” relationship, but, what distinguishes it from composition, is that the lifecycles of the objects are not tied. Both the entries can survive individually which means ending one entity will not affect the other entity. Both of them can exist independently of each other. Therefore, it is often referred to as week association.
Let’s take the example of a player and a team. A player who is a part of the team can exist even when the team ceases to exist. The main reason why you need Aggregation is to maintain code reusability.
This brings us to the end of this article where we have learned about Association in Java. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.
Do look out for other articles in this series that will explain the various other aspects of Java.
1. Object Oriented Programming
5. Java String
6. Java Array
8. Java Threads
9. Introduction to Java Servlets
11. Exception Handling in Java
12. Java Tutorial
14. Java Programs
15. Kotlin vs Java
16. Dependency Injection Using Spring Boot
22. Socket Programming In Java
25. Library Management System Project in Java
26. Trees in Java
28. Top Data Structures & Algorithms in Java
30. Top 55 Servlet Interview Questions
34. Java Collections Interview Questions and Answers
35. How to Handle Deadlock in Java?
36. Top 50 Java Collections Interview Questions You Need to Know
37. What is the concept of String Pool in Java?
38. What is the difference between C, C++, and Java?
39. Palindrome in Java- How to check a number or string?
40. Top MVC Interview Questions and Answers You Need to Know
41. Top 10 Applications of Java Programming Language
42. Deadlock in Java
43. Square and Square Root in Java
Originally published at https://www.edureka.co on October 15, 2019.