Association, Composition and Aggregation in Java

I was very bored at home yesterday, wanted to engage with something new. Suddenly thought about the old carom board and I searched for it in the store room.I found the set of coins but the board was damaged.

Is there any significance for that coins without that carom board?

No!

Which means these two have a special relationship.

The objects that we see around us may have a relationship. It can be one-to-one, one-to-many, many-to-one, and many-to-many.

Association signifies these kind of relationship between multiple objects and Aggregation and Composition are the two forms of Association.

Coins cannot logically exist without the carom board.This kind of relationship or association is called ‘Composition’. i.e. If one class owns the other class then the other class cannot meaningfully exist when it’s owner is destroyed.

For example plant and leaf; human and heart.

 

  • How it can be implemented in java ?

 

 class SIM {

                   private String serviceProvider;

                   private String type;

 

                public String getServiceProvider() {

                        return serviceProvider;

}

                public void set ServiceProvider(String serviceProvider) {

                        this.serviceProvider = serviceProvider;

}

                public String getType() {

                      return type;

}

              public void setType(String type) {

                       this.type = type;

}

}

class Mobile {

              private final SIM sim;

              public Mobile() {

sim = new SIM();

}

}

 public class Test {

                public static void main(String[] args) {

Mobile mobile = new Mobile();

}

}

 

Here when I create an object of Mobile, SIM object is automatically created. If I destroy the Mobile object then the SIM object has no significance in this scenario.

 

Now let’s look into the other type of association called Aggregation:

 

Aggregation is a kind of association where one class owns other class then the other class can exist even if the owner is destroyed.

For example College and Student. Even if College is closed the Student still exist.

So in the case of Aggregation, if the owner object is destroyed it does not really affect the associated object.

 

  • How it can be implemented in java ?

 

class Car {

               private String model;

               public String getModel() {

                       return model;

}

               public void setModel(String model) {

                        this.model = model;

}

}

 

class Driver {

                 private String name;

                 private int age;

                 private Car car;

 

               public String getName() {

                      return name;

}

             public void setName(String name) {

                     this.name = name;

}

             public int getAge() {

                     return age;

}

             public void setAge(int age) {

                      this.age = age;

}

           public Car getCar() {

                   return car;

}

          public void setCar(Car car) {

                   this.car = car;

}

}

 

public class TestDriver {

             public static void main(String[] args) {

Driver driver = new Driver();

driver.setName(“David”);

Car car = newCar();

car.setModel(“Toyota Camry”);

driver.setCar(new Car());

}

}

 

Here I have a class Driver and Driver has a Car(which means class Driver has a reference of another class Car). I created an object of class Driver and initialized its variables (including Car). Now if I destroy the Driver object, it does not really affect the Car which is the associated object. i.e. still the Car object has its significance.

 

UML notation for these kinds of relations are shown below:

For the example what I have mentioned above – the UML diagram looks like this.

Conclusion:

Composition is an association in which one class belongs to a group or collection.

This is a part of a whole relationship where a part cannot exist without a whole.

Therefore if a whole is deleted then all parts are also deleted.

 

If we can consider an order is a whole and line items are parts, then if the order is deleted then all corresponding line items for that order should be deleted.

So composition has a stronger relationship.

 

Aggregation is an association in which one class belongs to a group or collection. This is a part of a whole relationship where a part can exist without a whole.If we take a line item as a whole and product as a part, then if the line item is deleted then corresponding product doesn’t have to be deleted.

So aggregation has a weaker relationship.

 

Amrutha B

 

About the author: Amrutha is working as a Junior Consultant in jThread IT Training & Consultancy.

24 thoughts on “Association, Composition and Aggregation in Java

Leave a Reply to Ancy Alexander Cancel reply

Your email address will not be published. Required fields are marked *