Difference between association, aggregation and composition
BaabtraMentoringPartner
18,491 views
19 slides
May 15, 2013
Slide 1 of 19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
About This Presentation
No description available for this slideshow.
Size: 249.7 KB
Language: en
Added: May 15, 2013
Slides: 19 pages
Slide Content
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 @[email protected] www.facebook.com/ Abhishek H menon twitter.com/Abhishek H Menon in.linkedin.com/in/Abhishek H menon Phonenumber:9496519895
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 TEACHER COURSE 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