Вы находитесь на странице: 1из 7

Home All Tutorials Core Java OOPs Collections Java I/O JSON DBMS

OOPs concepts – What is Association in java? Search this website …


BY CHAITANYA SINGH | FILED UNDER: OOPS CONCEPT

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.

❮ Previous Next ❯

Comments

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

kiran says
DECEMBER 19, 2014 AT 10:30 AM

Superb Explanation with example. It’s awesome.

Reply

junaid ahmad says


JANUARY 13, 2015 AT 2:31 PM

crystal clear explaination

Reply

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

Bilawal says
APRIL 7, 2015 AT 12:03 PM

Easy and Superb Explanation

Reply

Rahul Mittal says


MAY 25, 2015 AT 12:41 PM
Please give an example of composition too.

Reply

mark says
MAY 28, 2016 AT 2:42 AM

What about one to one relation?

Reply

Zohaib says
DECEMBER 9, 2016 AT 5:29 AM

Can you please give another example of Association?

Reply

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
Your email address will not be published. Required fields are marked *

Comment
Name *

Email *

POST COMMENT

Java Tutorial

Java Index

Java Introduction

JVM - Java Virtual


Machine

First Java Program

Variables

Data Types

Operators

Java Control
Statements

Java If-else

Java Switch-Case

Java For loop

Java while loop

Java do-while loop

Continue statement

break statement

OOPs Concepts

OOPs Concepts

Constructor

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 Interview Q

MORE ...

Java 8 Features

Java 9 Features

Java Conversion

Java String

Exception handling

Java Multithreading

Java I/O

Java Serialization

Java Regex

Java AWT

Java Swing

Java Enum

Java Annotations
Copyright © 2012 – 2018 BeginnersBook . Privacy Policy . Sitemap