In this article we will discuss Association in Java. Association establishes relationship between two separate classes through their objects. The relationship can be one to one, One to many, many to one and many to many.
Association Example
class CarClass{ String carName; int carId; CarClass(String name, int id) { this.carName = name; this.carId = id; } } class Driver extends CarClass{ String driverName; Driver(String name, String cname, int cid){ super(cname, cid); this.driverName=name; } } class TransportCompany{ public static void main(String args[]) { Driver obj = new Driver("Andy", "Ford", 9988); System.out.println(obj.driverName+" is a driver of car Id: "+obj.carId); } }
Output:
Andy is a driver of car Id: 9988
In the above example, there is a one to one relationship(Association) between two classes: CarClass
and Driver
. Both the classes represent two separate entities.
Association vs Aggregation vs Composition
Lets discuss difference between Association, Aggregation and Composition:
Although all three are related terms, there are some major differences in the way they relate two classes. Association is a relationship between two separate classes and the association can be of any type say one to one, one to may etc. It joins two entirely separate entities.
Aggregation is a special form of association which is a unidirectional one way relationship between classes (or entities), for e.g. Wallet and Money classes. Wallet has Money but money doesn’t need to have Wallet necessarily so its a one directional relationship. In this relationship both the entries can survive if other one ends. In our example if Wallet class is not present, it does not mean that the Money class cannot exist.
Composition is a restricted form of Aggregation in which two entities (or you can say classes) are highly dependent on each other. For e.g. Human and Heart. A human needs heart to live and a heart needs a Human body to survive. In other words when the classes (entities) are dependent on each other and their life span are same (if one dies then another one too) then its a composition. Heart class has no sense if Human class is not present.
Saroj Kumar says
“In the above example, there is a one to one relationship(Association) between two classes: Car and Driver. Both the classes represents two separate entities.”
I want to know that how can you show the relationship between Car and Driver?
Since, in both class, there is no trace of reference of other class, we cannot say that both class are in relationship with each other.
Please clarify.
Thanks & Regards
Chaitanya Singh says
I have updated the example to make it more clear. Let me know if you still need help to understand the example.
kiran says
Superb Explanation with example. It’s awesome.
junaid ahmad says
crystal clear explaination
Annyomous says
Thank you for the explanation – came for a definition of association and got a lot more information than I was expected.
Bilawal says
Easy and Superb Explanation
Rahul Mittal says
Please give an example of composition too.
mark says
What about one to one relation?
Zohaib says
Can you please give another example of Association?
Mofis khan says
In association we should pass the object of one class to another class as argument but this association is not doing that instead using the getter and setter methods ,please can you provide the another example of this