Difference between Association, Composition and Aggregation in Java, UML and Object Oriented Program

In Object-oriented programming, one object is related to other to use functionality and service provided by that object. This relationship between two object is known as  association  in  object oriented general software design, and depicted by an arrow in Unified Modelling language or UML. Both Composition and Aggregation are form of association between two objects, but there is  subtle difference between composition and aggregation , which is also reflected by their UML notation. We refer association between two objects as composition, when one class  owns  other class and other class can not meaningfully exist, when it's owner destroyed, for example  Human  class is composition of several body parts including  Hand Leg  and  Heart . When human object dies, all it's body part ceased to exist meaningfully, this is one example of  Composition . Programmers often confuse between Association, Composition and Aggregation in Object oriented design discussions, this confusion also makes  difference between Association, Composition and Aggregation  one of the popular questions in Java Interviews, only after  difference between abstract class and interface  . Another example of  Composition  is  Car  and it's part e.g. engines, wheels etc. Individual parts of car can not function, when car is destroyed.  While in case of Aggregation, including object can exists without being part of main object e.g. a  Player  which is part of a  Team , can exists without team and can become part of other teams as well. Another example of  Aggregation  is  Student  in  School  class, when School closed, Student still exist and then can join another School or so.  In UML notation, composition is denoted by a  filled diamond , while aggregation is denoted by an  empty diamond , which shows their obvious difference in terms of strength of relationship. Composition is more stronger than Aggregation.  In Short, relationship between two objects is referred as association, and an association is known as composition when one object  owns  other, while an association is known as aggregation when one object  uses  other object. In this OOPS tutorial, we will see couple of more examples to understand difference between Association, Composition and Aggregation better.

An Example of Association, Composition and Aggregation in Java

Here is an example of composition and aggregation, in terms of Java Code. By looking at this code, you can gauge differences between these two. By the way, Composition is also very much preferred in object oriented design over inheritance, even Joshua bloach has stated its importance in classic book, Effective Java.

Composition :  Since Engine is  part-of  Car, relationship between them is Composition. Here is how they are implemented between Java classes.

public class Car {
    //final will make sure engine is initialized
    private final Engine engine;  
       
    public Car(){
       engine  = new Engine();
    }
}

class Engine {
    private String type;
}


Aggregation :  Since  Organization  has  Person  as employees, relationship between them is Aggregation. Here is how they look like in terms of Java classes

public class Organization {
    private List employees;
}

public class Person {
    private String name;   
}


UML Diagram of Association, Composition and Aggregation 
UML has different notations to denote aggregation, composition and association.  Association is denoted by simple arrow, while aggregation is denoted by  empty diamond head arrow and composition is denoted by filled diamond head arrow. When you draw UML diagram for two related class A and B, where A is associated with B then its denoted by A -> B. Similar way is used to show aggregation and composition between two classes. Here is UML notations for different kind of dependency between two classes.

Difference between Association, Aggregation and Composition


As, I said all three denotes relationship between object and only differ in their strength, you can also view them as below, where composition represent strongest form of relationship and association being the most general form.

Association vs Aggregation vs Composition



Association vs Composition vs Aggregation

Here is the list of differences between Composition and Aggregation in point format, for quick review. As I said key difference between them comes from the point that in case of  Composition , One object is  OWNER  of other object, while in case of aggregation, one object is just a  USER  or another object.

1) If A and B two classes are related to each other such that, B ceased to exist, when A is destroyed, then association between two object is known as  Composition . Example is  Car  and  Engine . While if A and B are associated with each other, such that B can exist without being associated with A, then this association in known as  Aggregation .

2) In case of Composition A owns B e.g.  Person  is owner of his  Hand Mind  and  Heart , while  in case of Aggregation, A uses B e.g.  Organization  uses  People  as employee.

3) In UML diagram Association is denoted by normal arrow head, while Composition is represented by filled diamond arrow head, and Aggregation is represented by empty diamond arrow head, As shown in below and attached diagram in third paragraph.

Association  A---->B
Composition  A-----<filled>B
Aggregation  A-----<>B

4) Aggregation is a lighter form of Composition, where sub-part object can meaningfully exits without main objects.

5) In Java, you can use  final keyword  to represent Composition. Since in Composition, Owner object expect part object to be available and functions, by making it  final , your provide guarantee that, when Owner will be created, this part object will exist. This is actually a  Java idiom to represent strong form of association  i.e. composition between two objects.

6) Another interesting word, which comes handy to understand difference between Composition and Aggregation in software design is  "part-of"  and  "has" . If one object is part-of another object e.g. Engine is part of Car, then association or relationship between them is Composition. On the other hand if one object just has another object e.g. Car has driver than it's Aggregation.

That's all on  difference between Association, Composition and Aggregation in UML, Java and Object oriented design . Since object oriented analysis is more about defining relationship between object, it's important to know what kind of relationship exists between them, composition and aggregation is a two way of representing relationship between two objects.

Reference: http://javarevisited.blogspot.sg/2014/02/ifference-between-association-vs-composition-vs-aggregation.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值