Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Difference between Association,
Aggregation and Composition
Name
@abhishekmenon121989@gmail
.com
www.facebook.com/
Abhishek H menon
twitter.com/Abhishek H
Menon
in.linkedin.com/in/Abhishek
H menon
Association,Aggregation and Composition
• They are the relationship among classes in
java.
– Association
– Aggregation
– Composition
Derivation diagram
Association
Composition
Aggregation
Association
• It is a relationship between objects.
• Defines the multiplicity between objects.
• We are aware about the relationship such as
one-one, one-many, many-one, many-many,
these terms defines the association between
objects.
• It represents a binary relationship in between
two objects that describes an activity.
Association
• E.g.
• public class Student{
/* data fields*/
private Course;
courseList;
/*Constructors*/
/*Methods*/
}
STUDENT TEACHERCOURSE
takes teach
Association
• public class Course{
/* data fields*/
private Student[];
classList;
Private Faculty faculty;
/*Constructors*/
/*Methods*/
}
• public class Faculty{
/* data fields*/
private Course[];
courseList;
/*Constructors*/
/*Methods*/
}
Difference between association and
aggregation
• Association is a relationship between two classes
where one class use another. But aggregation
describes a special type of an association.
• Aggregation is the relationship between two classes.
When object of one class has object of another, if
second is a part of first then we called that there is
an aggregation between two classes. Unlike
association, aggregation always insists a direction.
Aggregation
• It is special case of association.
• A directional association between objects.
• When an object “has-a” another object, then
you have got an aggregation in between them.
• Direction between them should be specified
which object contains the other object.
• It is also called ‘has-a’ relationship.
Aggregation(e.g.)
Circle
Operation O
double i
area(int radius)
Operation
Square i
Aggregation
• class Operation{
int square(int i){
return n*n;
}}
• class Circle{
double pi=3.14;
double area(int radius){
Operation O=new Operation();
int rsquare=O.square(radius);
return pi*rsquare;
}
Aggregation
• Another example: take an example of our
department and developers. A developer can
be only in one department like java,dotnet
etc.suppose if we delete the department
object, still the developer exists. This is a
‘has-a’ relation.
Composition
• It is a special case of aggregation.
• In more specific manner, a restricted aggregation
is called composition.
• When an object contains another object, if the
contained object cannot exist without the
existence of the container object, then it is called
composition.
• In both aggregation and composition, direction is
must. As the direction specifies, which object
contains the other object.
Composition
• If Object B is contained within Object A, then
Object A is responsible for the creation and
destruction of Object B. Unlike
aggregation, Object B cannot exist without Object
A.
Composition(e.g.)
import java.util.GregorianCalendar;
public class Student {
private String name;
private GregorianCalendar dateOfBirth;
public Student(String name, int day, int month, int year) {
this.name = name;
this.dateOfBirth = new GregorianCalendar(year, month, day);
}
//rest of Student class.. }
Composition
• As the student class is responsible for the creation of
the GregorianCalendar object it will also be
responsible for its destruction (i.e., once the Student
object no longer exists neither will the
GregorianCalendar object). Therefore the
relationship between the two classes is composition
because Student has-a GregorianCalendar and it also
controls its lifetime. The GreogrianCalender object
cannot exist without the Student object.
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us

Difference between association, aggregation and composition

  • 2.
    Disclaimer: This presentationis prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 3.
    Difference between Association, Aggregationand Composition Name @abhishekmenon121989@gmail .com www.facebook.com/ Abhishek H menon twitter.com/Abhishek H Menon in.linkedin.com/in/Abhishek H menon
  • 4.
    Association,Aggregation and Composition •They are the relationship among classes in java. – Association – Aggregation – Composition
  • 5.
  • 6.
    Association • It isa relationship between objects. • Defines the multiplicity between objects. • We are aware about the relationship such as one-one, one-many, many-one, many-many, these terms defines the association between objects. • It represents a binary relationship in between two objects that describes an activity.
  • 7.
    Association • E.g. • publicclass Student{ /* data fields*/ private Course; courseList; /*Constructors*/ /*Methods*/ } STUDENT TEACHERCOURSE takes teach
  • 8.
    Association • public classCourse{ /* data fields*/ private Student[]; classList; Private Faculty faculty; /*Constructors*/ /*Methods*/ } • public class Faculty{ /* data fields*/ private Course[]; courseList; /*Constructors*/ /*Methods*/ }
  • 9.
    Difference between associationand aggregation • Association is a relationship between two classes where one class use another. But aggregation describes a special type of an association. • Aggregation is the relationship between two classes. When object of one class has object of another, if second is a part of first then we called that there is an aggregation between two classes. Unlike association, aggregation always insists a direction.
  • 10.
    Aggregation • It isspecial case of association. • A directional association between objects. • When an object “has-a” another object, then you have got an aggregation in between them. • Direction between them should be specified which object contains the other object. • It is also called ‘has-a’ relationship.
  • 11.
  • 12.
    Aggregation • class Operation{ intsquare(int i){ return n*n; }} • class Circle{ double pi=3.14; double area(int radius){ Operation O=new Operation(); int rsquare=O.square(radius); return pi*rsquare; }
  • 13.
    Aggregation • Another example:take an example of our department and developers. A developer can be only in one department like java,dotnet etc.suppose if we delete the department object, still the developer exists. This is a ‘has-a’ relation.
  • 14.
    Composition • It isa special case of aggregation. • In more specific manner, a restricted aggregation is called composition. • When an object contains another object, if the contained object cannot exist without the existence of the container object, then it is called composition. • In both aggregation and composition, direction is must. As the direction specifies, which object contains the other object.
  • 15.
    Composition • If ObjectB is contained within Object A, then Object A is responsible for the creation and destruction of Object B. Unlike aggregation, Object B cannot exist without Object A.
  • 16.
    Composition(e.g.) import java.util.GregorianCalendar; public classStudent { private String name; private GregorianCalendar dateOfBirth; public Student(String name, int day, int month, int year) { this.name = name; this.dateOfBirth = new GregorianCalendar(year, month, day); } //rest of Student class.. }
  • 17.
    Composition • As thestudent class is responsible for the creation of the GregorianCalendar object it will also be responsible for its destruction (i.e., once the Student object no longer exists neither will the GregorianCalendar object). Therefore the relationship between the two classes is composition because Student has-a GregorianCalendar and it also controls its lifetime. The GreogrianCalender object cannot exist without the Student object.
  • 18.
    If this presentationhelped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 19.