Abhinav
Abhinav
Composition and
Aggregation in Java
Presented by:
Abhinaw Anand Bharadwaj
MCA/40021/24
Introduction to Object Relationships
Unidirectional Association:
This association is the one in which one class
is aware and associated with another class;
the reverse is not true. For example, the
Student class can be associated with the
LibraryCard class, for the association where
the student has a library card; a LibraryCard
does not need to ‘know about’ a Student.
Type of Association
Bidirectional Association:
In this type of association, the classes are
aware of each other and interact with one
another. For example, a Teacher class and a
Classroom class may be associated
bidirectionally; there would be a teacher
assigned to a classroom, and a classroom
would know to which teacher it is assigned.
ASSOCIATION CAN BE ONE-TO-ONE, ONE-
TO-MANY, MANY-TO-ONE, MANY-TO-MANY
Example:
The Teacher class and Subject
class are separate, and each
can exist independently.
Code: // class representing a
// Class representing a Subject
Teacher
class Subject {
class Teacher {
String subjectName;
String name;
class Room{
}
class House{
Private final List <Room> rooms;
}