Tutorials Hut

Association, Aggregation and Composition

Association

  • Association is a relation between two classes. To achieve association we used objects of classes.
  • Association can be of between one to one, one to many, many to one and many to many objects of classes.

One to One:
If single class object has some relation with another single object of a class then it is called one to one association.

One to Many:
If single class object has some relation with more than one object of a class then it is called one to many association.

Many to One:
If multiple objects of class has relation with one object of class then it is called many to one relationship.

Many to Many:
If multiple objects of a class has relation with another multiple objects of class then it is called many to many relationship.

Aggregation and composition are forms of association.

Aggregation

Aggregation is a form of association where one class has an object(s) of another class in such a way that destruction of any one of class object wont destroy another, they are loosely associated.

Aggregation is called has-a relation.

Example:
Army has AirForce and AirForce has Pilot, in this example destruction of one does not necessary means another can’t exist.

import java.util.ArrayList;
import java.util.List;

class Army {

final AirForce airForce;

Army(AirForce airForce) {
this.airForce = airForce;
}
}

class AirForce {

final List pilots;

AirForce(List pilots) {
this.pilots = pilots;
}
}

class Pilot {
String name;
int age;
}

public class AssociationExample {

public static void main(String[] args) {

Pilot pilot = new Pilot();
List pilots = new ArrayList();
pilots.add(pilot);

AirForce airForce = new AirForce(pilots);

Army army = new Army(airForce);
}
}

Composition

Composition is a form of association where one class has object(s) of another class in such a way that destruction of first class object leads to destruction of other class object available in first class. Composition is also called part of relation where object of one class resides inside object of another class and it is part of it. In composition objects are tightly coupled. Example: A HumanBody class having object of class Heart is an example of composition where destruction of HumanBody object will destroy Heart object as well, Heart can’t live without HumanBody.  
class Heart {

}

class HumanBody {
Heart heart = new Heart();
}

UML of Association, Aggregation and Composition

UML diagram of Association:

Association

 UML diagram of Aggregation:

UML diagram of Composition:

Composition

Generalization and Specialization

Generalization:
Generalization is a process where we define a parent or super class for smaller child classes and by doing that we are generalizing different smaller classes to one super or parent class.

Generalization is a bottom up approach.

Generalization can be achieved by creating sub classes and their one parent class.

Example:
Car class is generalized from ElectricCar and PetrolCar sub classes.

Specialization:

Specialization is a process where we define child or sub classes from one super class which are more specific and specialized in details.

Specialization is a top down approach.

Example:
Sub classes ElectricCar and PetrolCar from parent Car class is an example of specialization.

Diagram of Generalization and Specialization:

Generalization and Specialization

References

Reference article from oracle: UML

Recommended Articles To Read














  • Leave a Reply

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