Categories: Java

Association, Composition & Aggregation in Java

Association:
  • Association is a relation between two separate classes which establishes through their Objects. One-to-one, one-to-many, many-to-one, and many-to-many associations are all possible.
  • An Object communicates with another object to use its functionality and services in Object-Oriented programming.
  • Composition and Aggregation are the two forms of association.
Composition:
  • The strongest type of association is composition.
  • It represents part-of relationship.
  • If an object owns another object and the other object cannot exist without the owner object, the association is said to be composition.
Aggregation:
  • Aggregation is a weak association.
  • An association is said to be aggregation if both Objects can exist independently.
  • It represents Has-A’s relationship.
Aggregation example:
class Operation{  
 int square(int n){  
  return n*n;  
 }  
}  
  
class Circle{  
 Operation op;//aggregation  
 double pi=3.14;  
    
 double area(int radius){  
   op=new Operation();  
   int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).  
   return pi*rsquare;  
 }  
  
     
    
 public static void main(String args[]){  
   Circle c=new Circle();  
   double result=c.area(5);  
   System.out.println(result);  
 }  
}  
Output:
78.5

We’ve created a reference to the Operation class in the Circle class in this example.

Composition Example:
import java.io.*;   
import java.util.*;   
// class College  
class College {   
    public String name;   
    public String address;   
    College(String name, String address)   
    {   
        this.name = name;   
        this.address = address;   
    }   
}   
// University has more than one college.   
class University {   
    // reference to refer to list of college.   
    private final List<College> colleges;   
    University(List<College> colleges)  
    {  
        this.colleges = colleges;  
    }   
    // Getting total number of colleges  
    public List<College> getTotalCollegesInUniversity()   
    {   
        return colleges;   
    }   
}   
class CompositionExample {   
    public static void main(String[] args)   
    {   
        // Creating the Objects of College class.   
        College c1   
            = new College("XYZ Engineering College", "Kolkata");   
        College c2   
            = new College("ABC Engineering College", "Delhi");   
        College c3 = new College("Coderz College of Engineering & Management Sudies",   
                           "Bombay");   
        // Creating list which contains the no. of colleges.   
        List<College> college = new ArrayList<College>();   
        college.add(c1);   
        college.add(c2);   
        college.add(c3);   
        University university = new University(college);   
        List<College> colleges = university.getTotalCollegesInUniversity();   
        for (College cg : colleges) {   
            System.out.println("Name : " + cg.name   
                               + " and "  
                               + " Address : " + cg.address);   
        }   
    }   
}  
Output:
Name : XYZ Engineering College and  Address : Kolkata
Name : ABC Engineering College and  Address : Delhi
Name : Coderz College of Engineering & Management Sudies and  Address : Bombay

We create the College class, which contains variables such as name and address. We also create a class called University, which has a reference to the college list. There can be multiple collages at a university. As a result, if a university closes permanently, all colleges within that university must close as well, because colleges cannot exist without a university. Composition is the relationship between the university and the colleges.

Note: also read about the Inheritance in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago