BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

OOPs concepts – What is Association in java?

Last Updated: May 29, 2024 by Chaitanya Singh | Filed Under: java

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

  1. One-to-One Association: A single instance of one class is associated with a single instance of another class.
  2. One-to-Many Association: A single instance of one class is associated with multiple instances of another class.
  3. Many-to-One Association: Multiple instances of one class are associated with a single instance of another class.
  4. 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.

❮ PreviousNext ❯

Top Related Articles:

  1. Java – Static Class, Block, Methods and Variables
  2. What is new Keyword in Java
  3. Encapsulation in Java with example
  4. Servlet interview questions and answers
  5. Multithreading in java with examples

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Saroj Kumar says

    August 11, 2014 at 7:00 AM

    “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

    Reply
    • Chaitanya Singh says

      September 11, 2017 at 12:32 PM

      I have updated the example to make it more clear. Let me know if you still need help to understand the example.

      Reply
  2. kiran says

    December 19, 2014 at 10:30 AM

    Superb Explanation with example. It’s awesome.

    Reply
  3. junaid ahmad says

    January 13, 2015 at 2:31 PM

    crystal clear explaination

    Reply
  4. Annyomous says

    March 30, 2015 at 12:03 PM

    Thank you for the explanation – came for a definition of association and got a lot more information than I was expected.

    Reply
  5. Bilawal says

    April 7, 2015 at 12:03 PM

    Easy and Superb Explanation

    Reply
  6. Rahul Mittal says

    May 25, 2015 at 12:41 PM

    Please give an example of composition too.

    Reply
  7. mark says

    May 28, 2016 at 2:42 AM

    What about one to one relation?

    Reply
  8. Zohaib says

    December 9, 2016 at 5:29 AM

    Can you please give another example of Association?

    Reply
  9. Mofis khan says

    December 12, 2016 at 8:32 AM

    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

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap