Association, Aggregation & Composition,

Association

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. In Object-Oriented programming, an Object communicates to other Object to use functionality and services provided by that object. Composition and Aggregation are the two forms of association.

Program


//Java program to understand the concept of association
class School
{
    private String school_name;
    School(String school_name)
    {
        this.school_name = school_name;
    }
    public String getSchoolName()
    {
        return this.school_name;
    }
}

class Student
{
    private String student_name;
    Student(String student_name)
    {
        this.student_name = student_name;
    }
    public String getStudentName()
    {
        return this.student_name;
    }
}

public class Main
{
    public static void main(String args[])
    {
        School s = new School("Sarsvati Vidyalaya");
        Student st = new Student("Hansraj Rami");
        System.out.println(st.getStudentName() + " is student of " + s.getSchoolName());
        //school can have many number of students so it is one to many relationship
    }
}
  
  

Output


Hansraj Rami is student of Sarsvati Vidyalaya
  

Explanation

In above exmaple we have created two separate classes School and Student. school can have many number students so it is one to many relationship.

Aggregation

It is a special form of Association where:
  • It represents Has-A relationship.
  • It is a unidirectional association i.e. a one way relationship. For example, school can have students 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

Program


//java program to understand the concept of composition
class Circle
{
    // aggregation
    Square_operation s;
    double PI = 3.14;
    double area(int radius)
    {
        s = new Square_operation();
        int r_sq = s.square(radius);
        return PI*r_sq;
    }
}

class Square_operation
{
    int square(int r)
    {
        return r*r;
    }
}

public class Main
{
    public static void main(String args[])
    {
        Circle c = new Circle();
        System.out.println(c.area(10));
    }
}
  
  

Output


314.0
  

Explanation

Consider above example, Circle object contains informations such as PI. It contains one more object named Square_operation, which contains its own information such as radius.So, there is a HAS-A relationship between class Circle and Square_operation.

Why and when do we use Aggregation?

Code reuse is best achieved by aggregation. Inheritance should be used only if the relationship IS-A is maintained throughout the lifetime of the objects involved; otherwise, aggregation is the best choice.

Composition

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.

Program


//java program to understand the concept of composition
class Library
{
    //library can have no of books
    //book does not exist without library 
    // so there is strong relationship it is called composition
    Book b;
    Library(String bookname)
    {
        b = new Book(bookname);
        b.getBookInfo();
    }
}

class Book
{
    String bookname;

    Book(String bookname)
    {
        this.bookname = bookname;
    }

    public void getBookInfo()
    {
        System.out.println(bookname);
    }
}

public class Main
{
    public static void main(String args[])
    {
        Library l = new Library("Hemlet");
    }
}
  
  

Output


Hemlet
  

Explanation

In above example a library can have no. of books on same or different subjects. So, If Library gets destroyed then All books within that particular library will be destroyed. i.e. book can not exist without library. That’s why it is composition.

Difference between Association, Aggregation and Composition

In Object-oriented programming, one object is related to other to use functionality and service provided by that object. This relationship between two objects is known as the association in object oriented general software design and depicted by an arrow in Unified Modelling language or UML. Both Composition and Aggregation are the form of association between two objects, but there is a subtle difference between composition and aggregation, which is also reflected by their UML notation. We refer association between two objects as Composition, when one class owns other class and other class can not meaningfully exist, when it's owner destroyed, for example, Human class is a composition of several body parts including Hand, Leg and Heart. When human object dies, all it's body part ceased to exist meaningfully, this is one example of Composition.

Programmers often confuse between Association, Composition and Aggregation in Object oriented design discussions, this confusion also makes the difference between Association, Composition and Aggregation one of the popular questions in Java Interviews, only after the difference between abstract class and interface .

Another example of Composition is Car and it's part e.g. engines, wheels etc. Individual parts of the car can not function when a car is destroyed. While in the case of Aggregation, including object can exists without being part of the main object e.g. a Player which is part of a Team, can exist without a team and can become part of other teams as well.

Another example of Aggregation is Student in School class, when School closed, Student still exist and then can join another School or so. In UML notation, a composition is denoted by a filled diamond, while aggregation is denoted by an empty diamond, which shows their obvious difference in terms of strength of the relationship.

The composition is stronger than Aggregation. In Short, a relationship between two objects is referred as an association, and an association is known as composition when one object owns other while an association is known as aggregation when one object uses another object.

Comments

Popular posts from this blog

System.Environment

System.Console

Datatype and keyword