Table of Contents
How do you establish a connection between two classes when you are writing a Java Program? It’s easy. You can make use of a concept called association. It sounds interesting right? In this blog, let’s check out the Association in Java in detail.
What is Association in Java?
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.
Now, let’s understand about Association with an example.
As you can see, in this example program there are two classes, namely, states and cities. 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 in detail.
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.
It is a “belongs-to” type of association. It 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. 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, 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 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.