Difference between Composition and Aggregation

In Object oriented languages, Composition, Aggregation and Inheritance are the most common relationships exist between classes.

Composition in Java

Composition is has-a relationship. Also sometimes referred as non-separable part of the whole.
Composition is more stronger relationship compared to Aggregation. Here composed object cannot exist without composing object, and both objects have same life time.

As shown in below code snippet, object of B(i..e obj) cannot exist without object of B. Object of B gets created only along with object of A. Also obj1 does not continue to exist, after object of A gets destroyed.

class A{
B obj;
//… other data members and methods, of class A
}

Aggregation

Aggregation is also a has-a relationship. Also sometimes referred as separable part to whole.
Aggregation is a weaker relationship compared to Composition.
As shown in below code snippet, object of D(i..e obj1) can exist even before object of C gets created. Also obj1 may continue to exist even after object of C is destroyed.

class C{
D obj1;
//… other data members and methods, of class C
}
Composition is Strong Has-A relationship Aggregation is a Weak Has-A relationship.

To understand difference between Composition and Aggregation, lets us take an example of a College with few Departments, and each Department with Lecturers.

aggregation and composition
aggregation and composition

Relationship between College and Department is Composition, and relationship between Department and Lecturer is Aggregation.

class College
{
  private List depts;

  public College()
  {
    depts.add(new Department("Computer Dept"));
    depts.add(new Department("Electrical Dept"));
    depts.add(new Department("Electronics Dept"));
  }

  //...other methods in College class
}

A Department cannot exist without College, hence difference between them is Strong and hence Composition.
When College no more exist, Department cannot exist.

Relationship between Department and Lecturer can be Aggregation, as it is weaker. A Lecturer can exist, even before Department exist. And Lecturer may continue to exist even after Department gets destroyed.

class Department
{
  private Lecturer my_obj;

  public void setLecturer(Lecturer lect)
  {
    my_obj = lect;
  }
  //...other methods in Department class
}

Adapter Pattern with Inheritance

Adapter pattern makes two incompatible interfaces to work together. These Incompatible interfaces are not designed and developed for one another, and assuming they have evolved separately.

Adapter Pattern can be implemented using any of the below.

  1. Inheritance
  2. Composition

Implementing Adapter pattern with Inheritance

In below example NewArithmetic and Arithmetic are classes, which are incompatible. And they have been designed separately, and one is not designed for another.

Now ArithmeticAdapter is implemented to make NewArithmetic and Arithmetic classes, compatible with each other.

Here ArithmeticAdapter inherits from Adaptee, i..e NewArithmetic

public class NewArithmetic
{
    public float add_values(float val1, float val2)
    {
        return val1+val2;
    }

    public float sub_values(float val1, float val2)
    {
        return val1-val2;
    }
}

public class ArithmeticAdapter extends NewArithmetic{
    public int add_adptr(int val1, int val2)
    {
        return (int)add_values(val1,val2);
    }

    public int sub_adptr(float val1, float val2)
    {
        return (int)sub_values(val1,val2);
    }
}

public class Arithmetic
{
    public void add(int val1,int val2)
    {
        System.out.println("Added Result:"+new ArithmeticAdapter().add_adptr(val1, val2));
    }

    public void sub(int val1,int val2)
    {
        System.out.println("Subtraction Result:"+new ArithmeticAdapter().sub_adptr(val1, val2));
    }
}

public class AdapterPattern {
    public static void main(String[] args) {
        Arithmetic obj = new Arithmetic();

        obj.add(23, 42);
        obj.sub(46, 21);
    }

}

Output:
Added Result:65
Subtraction Result:25

You may also like to read: Adapter Pattern with Composition