0% found this document useful (0 votes)
27 views17 pages

Abhinav

Uploaded by

inq33108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views17 pages

Abhinav

Uploaded by

inq33108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Association,

Composition and
Aggregation in Java

Presented by:
Abhinaw Anand Bharadwaj
MCA/40021/24
Introduction to Object Relationships

 In Java, objects interact with each other


through various types of relationships. These
relationships help in structuring a robust and
reusable code base by defining how different
classes connect. The main relationships in
Java are Association, Aggregation, and
Composition.
Definition of Association in Java

 Association is a relation between two separate


classes which establishes through their Objects.

 Itshows how objects of two classes are


associated to each other.

 We can describe the Association as a HAS-A


relationship between the classes.
Type of Association

 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

 1.One-to-One: Each object of one class is


associated with a single object of another class.
 2.One-to-Many: An object of one class is
associated with multiple objects of another class.
 3.Many-to-One: Many objects of one class are
associated with a single object of another class.
 4.Many-to-Many: Multiple objects of one class are
associated with multiple objects of another class.
Example of Association in Java

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;

 public Teacher(String name) {  public Subject(String subjectName) {


 this.name = name;  this.subjectName = subjectName;
 }  }
 }  }
When to Use Association

 Oneclass needs to interact with or use the


functionality of another class..

 User need to represent a many-to-many


relationship
Aggregation

 Aggregation is a form of Association, often


described as a 'has-a' relationship. It
represents a whole-part relationship where
the part can exist independently of the
whole. This means the lifecycle of the part is
not dependent on the whole
Aggregation Characteristics

 1.The parts are not dependent on the whole


object’s lifecycle.
 2. Aggregation allows a class to reuse other
classes' objects and functionalities without
needing ownership over the objects.
 3. Represented by a hollow diamond in UML
diagrams.
Example of Aggregation in Java
 Class MusicPlayer{
 public void play(){
 }
 }
 Class Car{
 String model;
 MusicPlayer musicplayer;
 }

 Car has an object reference muscplayer ,so relationahip is Car HAS-A


Musicplayer.
 If any of the classes get destroy it will not affect the other.
When to use Aggregation?

 Code reuse is also best achieved by


aggregation when there is no is-a
relationship.
• Inheritance should be used only if the
relationship is-a is maintained throughout
the lifetime of the objects involved;
otherwise, aggregation is the best choice.
Composition

 Composition is a strong form of Aggregation


where the parts are strongly dependent on
the whole. It is a 'part-of' relationship. In
Composition, if the whole is destroyed, the
parts are destroyed too, as they do not exist
independently.
Example of Composition in Java

class Room{

}
class House{
Private final List <Room> rooms;
}

 Here if we delete the house , the rooms will


automatically be deleted. So,house and room are
dependent on each other
When to use Composition?

• Has-a Relationship: Use composition when one


class logically "has-a" relationship with another.
For example, a computer "has" processor or a car
"has" an engine.
• Encapsulation:Composition allows for better
encapsulation by hiding the details of the
composed object from the outside world, offering
greater control over the internals of the system.
References and Research

 Association, Composition and Aggregation in Java -


GeeksforGeeks
 Association, Composition and Aggregation in Java -
Scaler Topics
 ChatGPT
 Difference Between Aggregation and Composition in
Java - Java point
Thank you

You might also like