Java Concepts Explained Part 6
Java Concepts Explained Part 6
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:
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.
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
“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
Reply
kiran says
DECEMBER 19, 2014 AT 10:30 AM
Reply
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
Reply
Reply
mark says
MAY 28, 2016 AT 2:42 AM
Reply
Zohaib says
DECEMBER 9, 2016 AT 5:29 AM
Reply
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
Variables
Data Types
Operators
Java Control
Statements
Java If-else
Java Switch-Case
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
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