Saturday 27 May 2017

Association, Aggregation and Composition

Association is widely used oops concept and questions on this topic is very frequent in interviews.

Association also know as "HAS-A" relationship. It is dependency of one class into another class. HAS-A means one class has reference(s) of other classes(es) inside it in order to serve it's responsibility.


For example suppose we have Car but in order to run a car we needs wheels. It means Car object has dependency on Wheels object to serve it's responsibility. Whenever we have to build a car we need to pass wheels to it. Now we can say that Car  and Wheels share HAS-A relationship.


We can further divide Association in two categories :

1. Aggregation: In Aggregation object of a child class does exist without the object of it's parent class.
Example:  See at line number 2, we have created wheel object and passed reference of same to car object. At line number 6 we are setting null into car object it means car object doesn't exist any more, but still we can access wheel object directly at line number 8
So we can clearly say that car object has dependency on wheel object. As we have implemented the relationship in aggregation fashion in below code so wheel object can exist even without the object of parent car class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class TestAggregation {
 public static void main(String[] args) {
  Wheel wheel = new Wheel("Medium");
  Car car = new Car(wheel, "Car1");
  System.out.println("Wheel Size::"+car.wheel.wheelSize); //Here we can access the wheel via car object.
  car = null;
  
  System.out.println("Wheel Size::"+wheel.wheelSize); //We can still access the wheel without car object.
 }
 
}

class Car{
 String carName;
 Wheel wheel;
 
 public Car(Wheel wheel, String carName){
  this.wheel = wheel;
  this.carName = carName;
 }
}

class Wheel{
 String wheelSize;
 public Wheel(String wheelSize){
  this.wheelSize = wheelSize;
 }
}
2.Composition: In Composition object of child class can not't exist without object of it's parent class.
Example: See at line number 17, Here we have created wheel object inside car class instead of passing it to car object from outside as we did above. In this case once car object will destroy at line number 5 we won't be able to access wheel object at line number 7.
So we can clearly say that the car object has dependency on wheel object. As we have implemented relationship in composition fashion in below code so wheel object can not exist without the object of parent car class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class TestComposition {
 public static void main(String[] args) {
  Car car = new Car("Car1");
  System.out.println("Wheel Size::"+car.wheel.wheelSize); //Here we can access the wheel via car object.
  car = null;
  
  System.out.println("Wheel Size::"+car.wheel.wheelSize); //We can't access the wheel as car destroyed.
 }
 
}

class Car{
 String carName;
 final Wheel wheel;
 
 public Car(String carName){
  wheel = new Wheel("Medium");
  this.carName = carName;
 }
}

class Wheel{
 String wheelSize;
 public Wheel(String wheelSize){
  this.wheelSize = wheelSize;
 }
}
So we can say that Composition is more strict form of Association than Aggregation.
Now question comes how to decide between Aggregation or Composition, when to use Composition or Aggregation?

It all depends on your business requirements like how your objects are related to each other, scope of your objects etc. There is no hard and first rule for when to use what.


No comments:

Post a Comment