Association in JavaAssociation in Java defines the connection between two classes that are set up through their objects. Association manages one-to-one, one-to-many, and many-to-many relationships. In Java, the multiplicity between objects is defined by the Association. It shows how objects communicate with each other and how they use the functionality and services provided by that communicated object. Association manages one-to-one, one-to-many, many-to-one and many-to-many relationships. Let's take an example of each relationship to manage by the Association.
Let's takes an example of an Association to understand how it works in Java. AssociationExample.javaOutput: DescriptionIn the above example, we create two separate classes, i.e., Person and Mobile, associated through their objects. A person can have many mobile numbers, so it defines the one-to-many relationship. Types of AssociationIn Java, two types of Association are possible:
1) IS-A AssociationThe IS-A Association is also referred to as Inheritance. We all know about Inheritance in Java and if you don't know about it, click here. 2) HAS-A AssociationThe HAS-A Association is further classified into two parts, i.e., Aggregation and Composition. Let's understand the difference between both of them one by one. 1) AggregationIn Java, the Aggregation association defines the HAS-A relationship. Aggregation follows the one-to-one or one-way relationship. If two entities are in the aggregation composition, and one entity fails due to some error, it will not affect the other entity. Let's take the example of a toy and its battery. The battery belongs to a toy, and if the toy breaks and deletes from our database, the battery will still remaining in our database, and it may still be working. So in Aggregation, objects always have their own lifecycles when the ownership exists there. AggregationExample Output: Description: In the above example, there is a college that has several courses like BSC-CS, MCA, and Poly. Every course has several students, so we make a College class that has a reference to the object or list of objects of the Course class. That means College class is associated with Course class through the objects. Course class also has a reference to the object or list of objects of Student class means it is associated with Student class through its object and defines the HAS-A relationship. 2) CompositionA restricted form of the Aggregation where the entities are strongly dependent on each other. Unlike Aggregation, Composition represents the part-of relationship. When there is an aggregation between two entities, the aggregate object can exist without the other entity, but in the case of Composition, the composed object can't exist. To learn more about Composition, click here. Let's take an example to understand the concept of Composition. We create a class Mobile that contains variables, i.e., name, ram and rom. We also create a class MobileStore that has a reference to refer to the list of mobiles. A mobile store can have more than one mobile. So, if a mobile store is destroyed, then all mobiles within that particular mobile store will also be destroyed because mobiles cannot exist without a mobile store. The relationship between the mobile store and mobiles is Composition. CompositionExample.java: Output: Next TopicBig data Java vs Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India