Association is an important concept of object-oriented programming. In this article, we will discuss what is an Association in Java with the help of examples and programs. Association is a process of establishing 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 Program in Java
The Driver
class has a one-to-many association with the Car
class. A Driver
object can be associated with multiple Car
objects.
import java.util.ArrayList; import java.util.List; class Car { private String carName; private int carId; public Car(String carName, int carId) { this.carName = carName; this.carId = carId; } public String getCarName() { return carName; } public int getCarId() { return carId; } } class Driver { private String driverName; private List<Car> cars; public Driver(String driverName) { this.driverName = driverName; this.cars = new ArrayList<>(); } public String getDriverName() { return driverName; } public void addCar(Car car) { cars.add(car); } public List<Car> getCars() { return cars; } } public class TransportCompany { public static void main(String[] args) { Driver driver1 = new Driver("Andy"); Car car1 = new Car("Ford", 9988); Car car2 = new Car("Toyota", 8877); driver1.addCar(car1); driver1.addCar(car2); System.out.println(driver1.getDriverName() + " is a driver of the following cars:"); for (Car car : driver1.getCars()) { System.out.println("Car Name: " + car.getCarName() + ", Car ID: " + car.getCarId()); } } }
Output:
Andy is a driver of the following cars:
Car Name: Ford, Car ID: 9988
Car Name: Toyota, Car ID: 8877
One-to-Many Association: This type of association represents that one instance of a class (in this case, Driver
) is associated with multiple instances of another class (in this case, Car
).
Types of Association
- One-to-One Association: A single instance of one class is associated with a single instance of another class.
- One-to-Many Association: A single instance of one class is associated with multiple instances of another class.
- Many-to-One Association: Multiple instances of one class are associated with a single instance of another class.
- Many-to-Many Association: Multiple instances of one class are associated with multiple instances of another class.
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