You are on page 1of 22

Association

in Java

Dr.Rab Nawaz Jadoon


Department of Computer Science Assistant Professor
COMSATS University, Abbottabad
DCS Pakistan
COMSATS University, Islamabad
(Abbottabad Campus)

Object Oriented Programming (OOP)


Association
◼ Association refers to the relationship between
multiple objects.
❖ It refers to how objects are related to each other and
how they are using each other's functionality.

Department of Computer Science 2


Example program 1

Department of Computer Science 3


Java application class

Andy is a driver of car Id: 9988

Note:
1. In the above example, there is a one to one relationship(Association)
between two classes: CarClass and Driver.
2. Both the classes represent two separate entities.

Department of Computer Science 4


Example Program 2

Department of Computer Science 5


Java application class

Note:
1. In above example two separate classes Bank and
Employee are associated through their Objects.
2. Bank can have many employees, So it is a one-to-
many relationship.

Department of Computer Science 6


Type s of Association
◼ There are two forms of association
❖ Aggregation
❖ Composition

Department of Computer Science 7


1. Aggregation in Java
◼ If a class have an entity reference, it is known
as Aggregation.
◼ Aggregation represents HAS-A relationship.
❖ Consider a situation, Employee object contains many
information such as id, name, emailId etc.
❖ It contains one more object named address, which
contains its own information's such as city, state,
country, zipcode etc. as given below.
Note:
In such case, Employee has an entity
reference address,
so relationship is Employee HAS-A
address.

Department of Computer Science 8


Cont…
◼ Aggregation is a weak association.
◼ An association is said to be aggregation if both
Objects can exist independently.
❖ For example, a Team object and a Player object.
➢ The team contains multiple players, but a player can exist
without a team.

Department of Computer Science 9


Aggregation Example 1
Circle
Operation op;
Double PI;
Area(int radius);

Operation
Square(int n);

Output:
78.5
Department of Computer Science 10
Aggregation Example 2
◼ In this example, Employee has an object of
Address, address object contains its own
information such as city, state, country etc.
❖ In such case relationship is Employee HAS-A
address.

//Address.java

Department of Computer Science 11


Cont…

//Emp.java

Department of Computer Science 12


Cont…

//AggregationTest.java
public class AggregationTest
{
public static void main(String[] args)
{
Address address1=new Address(“Abbottabad",“KPK",“Pakistan");
Address address2=new Address(“Haipur",“KPK",“Pakistan");
Emp e=new Emp(111,“Abdullah",address1);
Emp e2=new Emp(112,“KaleemUllah",address2);

e.display();
e2.display();

}
Output:
} 111 Abdullah
Abbottabad KPK Pakistan
112 KaleemUllah
Haripur KPK Pakistan

Department of Computer Science 13


2. Composition in Java
◼ The composition is the strong type of
association.
❖ An association is said to composition if an Object
owns another object and another object cannot exist
without the owner object.
➢ Consider the case of Human having a heart. Here Human
object contains the heart and heart cannot exist without
Human.
➢ 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.

Department of Computer Science 14


Example
◼ In above example a library can have no.
of books on same or different subjects.
❖ So, If Library gets destroyed then All books within
that particular library will be destroyed. i.e. book can
not exist without library.
❖ That’s why it is composition.
➢ Example Code on the next slide

Department of Computer Science 15


Example Code (composition)

Department of Computer Science 16


Java main application class

Department of Computer Science 17


Aggregation vs Composition
◼ Dependency:
❖ Aggregation implies a relationship where the
child can exist independently of the parent.
➢ For example, Bank and Employee, delete the Bank and the
Employee still exist. whereas Composition implies a
relationship where the child cannot exist independent of
the parent. Example: Human and heart, heart don’t exist
separate to a Human
◼ Type of Relationship:
❖ Aggregation relation is “has-a” and composition
is “part-of” relation.
◼ Type of association:
❖ Composition is a strong Association whereas
Aggregation is a weak Association.
Department of Computer Science 18
UML Notation of Composition
◼ Composite aggregation is described as a binary
association decorated with a filled black
diamond at the aggregate (whole) end.

❖ The folder could contain many files, while each File


has exactly one Folder parent.
❖ If a folder is deleted, all contained files are removed
as well.

Department of Computer Science 19


UML Notation of Aggregation
◼ Aggregation is described as a binary association
decorated with an un-filled diamond.

❖ Car may have passengers; they come and go.

Department of Computer Science 20


Association, Aggregation and
Composition - UML

Department of Computer Science 21


Department of Computer Science 22

You might also like