Aggregation vs Composition

  • Dependency: Aggregation implies a relationship where the child can exist independently of the parent. For example, Bank and Employee, delete the Bank and the Employee still exist. whereas Composition implies a relationship where the child cannot exist independent of the parent. Example: Human and heart, heart don’t exist separate to a Human
  • Type of Relationship: Aggregation represents “has-a” relationship whereas, Composition represents “part-of” relationship.
  • Type of association: Composition is a strong Association whereas, Aggregation is a weak Association.

Example for Composition:

Java
// Java Program to Illustrate
// Composition

// Importing I/O classes
import java.io.*;

// Class 1
// Engine class which will be used by car.
class Engine {
  
    // Method to start the engine
    public void work()
    {
        // Print statement whenever this method is called
        System.out.println(
            "Engine of car has been started ");
    }
}

// Class 2
// Car class
class Car {

    // For a car to move,
    // it needs to have an engine.
    private final Engine engine;

    // Constructor of this class
    public Car()
    {
        this.engine = new Engine();   // Composition
    }

    // Method
    // Car start moving by starting engine
    public void move()
    {
        engine.work();
          System.out.println("Car is moving");
    }
}

// Class 3
// Main class
class CompositionCarExample {

    // Main driver method
    public static void main(String[] args)
    {
        // Creating a car object, 
        // which will also initializes the engine class object
        Car car = new Car();

        // Making car to move by calling
        // move() method inside main()
        car.move();
    }
}

Output
Engine of car has been started 
Car is moving

Explanation of the above Program:

In the above case of composition, the Car owns the Engine object (i.e. Engine cannot exist without the Car object).

Example for Aggregation:

Java
// Java Program to illustrate 
// Aggregation

import java.io.*;

// Class 1
// Engine class
class Engine {
  
      // attributes of Engine
      private String engineType;
      
    // Constructor
      public Engine(String engineType) {
          this.engineType = engineType;
    }      
  
      public String getEngineType() {
      return engineType;
    }
      
    // Method to start the engine
    public void work()
    {
        // Print statement whenever this method is called
        System.out.println("Engine of car has been started");
    }
}

// Class 2
// Car class
class Car {

    // For a car to move,
    // it needs to have an engine.
    private Engine engine;
    
      // Constructor
    public Car(Engine engine)
    {
        this.engine = engine;   // Aggregation
    }

    // Method
    // Car start moving by starting engine
    public void move()
    {
          if (engine != null) {
              engine.work();
              System.out.println("Car is moving");
        }
          else {
              System.out.println("Car cannot start without engine");
        }
    }
}

// Class 3
// Main class
class AggregationCarExample {
    public static void main (String[] args) {
          
          // Creating an independent Engine Object
          Engine engine = new Engine("V10");
      
          // Creating a Car object, by passing the engine
          // such that, Car "has" Engine
          Car car = new Car(engine);
          
          car.move();
        System.out.println("Engine Type: " + engine.getEngineType());
    }
}

Output
Engine of car has been started
Car is moving
Engine Type: V10

Explanation of the above Program:

In the above case of aggregation, the the Engine object is independent of the Car object (i.e. Engine can exist without the Car object).



Association, Composition and Aggregation in Java

Association is a relation between two separate classes which is established through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many. In Object-Oriented programming, an Object communicates to another object to use functionality and services provided by that object. Composition and Aggregation are the two forms of association. 

Similar Reads

Association

To understand the concept association, refer to the below example....

Aggregation

It is a special form of Association where:...

Composition

Composition is a restricted form of Aggregation in which two entities are highly dependent on each other....

Aggregation vs Composition

Dependency: Aggregation implies a relationship where the child can exist independently of the parent. For example, Bank and Employee, delete the Bank and the Employee still exist. whereas Composition implies a relationship where the child cannot exist independent of the parent. Example: Human and heart, heart don’t exist separate to a HumanType of Relationship: Aggregation represents “has-a” relationship whereas, Composition represents “part-of” relationship.Type of association: Composition is a strong Association whereas, Aggregation is a weak Association....