Aggregation and Composition in Java Code

23,688

Solution 1

Consider a simple example of a LinkedList class. A LinkedList is made up of Nodes. Here LinkedList class is the owning object. When it is destroyed, all the Nodes contained in it are wiped off. If a Node object is deleted from the list, the LinkedList object will still exist.

Composition is implemented such that an object contains another object.

class LinkedList {

 Node head;
 int size; 
}

This is composition.

Another example, consider a Circle class which has a Point class which is used to define the coordinates of its center. If the Circle is deleted its center is deleted along with it.

class Circle {

    private float radius;
    private Point center;

    public Circle(Point center, float radius) {
        this.center = center;
        this.radius = radius;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public void setCenter(Point center) {
        this.center = center;
    }

    public float getRadius() {
        return radius;
    }

    public Point getCenter() {
        return center;
    }

    class Point {
        private int x;
        private int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;

        }

        public void setY(int y) {
            this.y = y;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public int getX() {
            return x;
        }
    }

    @Override
    public String toString() {

        return "Center: " + "(" + center.getX() + "," + center.getY() + ")" + "\nRadius: " + this.getRadius()
                + " centimeters";
    }

}

Example of composition from java collections api : HashMap has an Entry class. If the HashMap object is deleted all the Entry objects contained in the map are deleted along with it.

Aggregation is also a type of Object Composition but not as strong as Composition. It defines "HAS A" relationship. Put in very simple words if Class A has a reference of Class B and removing B doesn't affect the existence of A then it is aggregation. You must have used it at some point.

Solution 2

A would like to add additional information to explained answers

Aggregation is a special form of Assosation (Association is relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many)

  • Aggregation represents Has-A relationship.
  • It is a unidirectional association i.e. a one way relationship. For example, department can have employees but vice versa is not possible and thus unidirectional in nature.
  • In Aggregation, both the entries can survive individually which means ending one entity will not effect the other entity.

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

  • It represents part-of relationship.
  • In composition, both the entities are dependent on each other.
  • When there is a composition between two entities, the composed object cannot exist without the other entity.

Aggregation vs Composition

- Dependency: Aggregation implies a relationship where the child can exist independently of the parent. For example, Department and Employee, delete the Department 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 relation is “has-a” and composition is “part-of” relation.

- Type of association: Composition is a strong Association whereas Aggregation is a weak Association.

Reference: https://www.geeksforgeeks.org/association-composition-aggregation-java/

Share:
23,688
Nirav Kamani
Author by

Nirav Kamani

Updated on March 24, 2020

Comments

  • Nirav Kamani
    Nirav Kamani over 4 years

    As the Aggregation and Composition is related Association or we can say it gives the understanding of relationship between object or anything else.

    I posted this question because i asked one question in Interview that what is the Composition and Aggregation.

    So as per my understanding i given my idea which is as follow.

    http://www.coderanch.com/t/522414/java/java/Association-Aggregation-Composition

    Aggregation, Association and Composition

    Association vs. Aggregation vs. Composition in Java

    and also visited much more.

    and my basic explanation was related that Aggregation indicates loose relationship while Composition indicates Strong relationship and clear explanation related to this.

    But the Interviewer Insulted me and said this is the theoretical concept about which you saying i want the perfect Java code in which how they differ and also told if i will give you one small Application then how would you identify that this is the Aggregation and this is the Composition?

    So now i want to understand pure Technical Concept and Java code in which how they differ and what it is?