Composition vs Aggregation explained with Java will show you what is Composition and what is Aggregation? In simple terms, both Composition and Aggregation are Associations. Composition means “Strong Has-A relationship”, whereas, Aggregation means “Weak Has-A relationship”.

Composition implies a relationship where the child cannot exist independently without the parent. Example: House (parent) and Room (child). Room doesn’t exist separately without a House.

Aggregation implies a relationship where the child can exist independently without the parent. Example: Class (parent) and Student (child). Delete the Class and the Student still exists.

Here in this example you will see the Composition vs Aggregation based on Car and Engine.

Let’s say you have following EngineSpecification class that specifies what are the requirements for an Engine of the Car.

public class EngineSpecification {
 
    private String engineType;
    private String pistonOrientation;
    private String displacement;
    private String torque;
 
    public EngineSpecification(String engineType, String pistonOrientation, String displacement, String torque) {
        this.engineType = engineType;
        this.pistonOrientation = pistonOrientation;
        this.displacement = displacement;
        this.torque = torque;
    }
 
    public String getEngineType() {
        return engineType;
    }
 
    public String getPistonOrientation() {
        return pistonOrientation;
    }
 
    public String getDisplacement() {
        return displacement;
    }
 
    public String getTorque() {
        return torque;
    }
 
}

Composition

In case of composition, the Engine is completely encapsulated by the Car. There is no way for the outside world to get a reference to the Engine. The Engine lives and dies with the car. Let’s see in the following class:

public class Car {
 
    private final Engine engine;
 
    public Car(EngineSpecification engineSpecification) {
        this.engine = new Engine(engineSpecification);
    }
 
    void move() {
        engine.work();
        System.out.println("Started moving the Car");
    }
 
    private class Engine {
 
        private final EngineSpecification engineSpecification;
 
        public Engine(EngineSpecification engineSpecification) {
            this.engineSpecification = engineSpecification;
        }
 
        public void work() {
            String engineType = engineSpecification.getEngineType();
            String pistonOrientation = engineSpecification.getPistonOrientation();
            String displacement = engineSpecification.getDisplacement();
            String torque = engineSpecification.getTorque();
 
            System.out.println("Engine Specifications:: Engine Type => " + engineType + ", Piston Orientation => " + pistonOrientation + ", Displacement => " + displacement + ", Torque => " + torque);
 
            // ...
            System.out.println("Start the Engine using specifications");
        }
    }
 
}

In the above class we it shows composition as strong association (a Car makes no sense without an Engine).

Testing

public class Composition {
 
    public static void main(String[] args) {
        EngineSpecification engineSpecification = new EngineSpecification("V8", "Right", "1200 cc", "200 ld-ft");
        Car car = new Car(engineSpecification);
        car.move();
    }
 
}

Output

Engine Specifications:: Engine Type => V8, Piston Orientation => Right, Displacement => 1200 cc, Torque => 200 ld-ft
Start the Engine using specifications
Started moving the Car

Aggregation

With aggregation, the Car also performs its functions through an Engine, but the Engine is not always an internal part of the Car. Engines may be swapped, or even completely removed. Not only that, but the outside world can still have a reference to the Engine, and tinker with it regardless of whether it’s in the Car.

Let’s say we have the following Engine class

public class Engine {
 
    private final EngineSpecification engineSpecification;
 
    public Engine(EngineSpecification engineSpecification) {
        this.engineSpecification = engineSpecification;
    }
 
    public void work() {
        String engineType = engineSpecification.getEngineType();
        String pistonOrientation = engineSpecification.getPistonOrientation();
        String displacement = engineSpecification.getDisplacement();
        String torque = engineSpecification.getTorque();
 
        System.out.println("Engine Specifications:: Engine Type => " + engineType + ", Piston Orientation => " + pistonOrientation + ", Displacement => " + displacement + ", Torque => " + torque);
 
        // ...
        System.out.println("Start the Engine using specifications");
    }
 
}

Now we have the following Car class

public class Car {
 
    private Engine engine;
 
    public void setEngine(Engine engine) {
        this.engine = engine;
    }
 
    void move() {
        engine.work();
        System.out.println("Started moving the Car");
    }
 
}

In the above Car class we see Aggregation as a weak association (a Car without an Engine makes totally sense, it doesn’t even need one in its constructor).

Testing

public class Aggregation {
 
    public static void main(String[] args) {
        EngineSpecification engineSpecification = new EngineSpecification("V8", "Right", "1200 cc", "200 ld-ft");
        Engine engine = new Engine(engineSpecification);
        Car car = new Car();
        car.setEngine(engine);
        car.move();
    }
 
}

Output

Engine Specifications:: Engine Type => V8, Piston Orientation => Right, Displacement => 1200 cc, Torque => 200 ld-ft
Start the Engine using specifications
Started moving the Car

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials | TwitterFacebook Google PlusLinkedin | Reddit

Leave a Reply

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