Home  >  Article  >  Java  >  Composition and Aggregation in Java

Composition and Aggregation in Java

王林
王林Original
2024-08-30 15:15:151011browse

Java provides the different relationships between the objects that can be considered real-life and programming. Sometimes it is very difficult to understand, or we can say that to implement the relationships. So java provides the composition and aggregation relationship to the user. In composition, we can build a relationship that we call “belongs to” relationship; logically, we can say that one object is larger than other objects. In aggregation, we can build a relation that we call a “has a” relationship, in which every object acts independently from each other. Aggregation relation we consider as weak association and composition we consider as a strong association.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of Composition and Aggregation

Syntax of composition as follows:

public class building {
private final Room room;
public building () {
room = new room ();
}
}
class Room { }

Explanation:

  • In the above syntax, we consider an example of building, so first, we create the class with the name building, and the room is the mandatory part of the building as shown in the above syntax.
  • This is the syntax of the composition relationship, and here we show it belongs to the relationship.

Syntax of Aggregation as follows:

public class student {
private List students;
public student () {
students = new list_students();
}
}
class student { }

Explanation:

  • In the above syntax, we consider the student example, in which first we create a public class with the name as a student, and after that, we create the list of students because students can be 0 or more as shown in the above syntax.
  • Here we also show the “has a” relationship between the objects.

How do Composition and Aggregation work in Java?

Given below shows how composition and aggregation work in java:

Composition is used to specify the “belongs to” of a relationship. It implies that one of the items is intelligently bigger than the other items, or we can say object. For example, consider a room as part of a building, or we can say a building has a room. So fundamentally, regardless of whether we call it “belongs to” or “has-a” is just a matter of perspective.

Basically, the composition is a strong relationship in light of the fact that the containing object claims it. In this manner, when we remove the object lifecycle, it means that when we remove the parent node, then automatically it removes the child nodes. For example, we can consider when we destroy rooms; then the building is also destroyed. Note that doesn’t mean that the containing object can’t exist with any of its parts. For instance, we can destroy all of the dividers inside a structure, henceforth obliterating the rooms. Be that as it may, the structure will, in any case, exist. As far as cardinality, a containing article can have however many parts as we need. Nonetheless, the entirety of the parts needs to have precisely one compartment.

Working of how aggregation works in java as follows:

We know that composition “has a” relationship. Similarly, aggregation also has a “Has a” relationship. The basic difference is that the aggregation does include the parent node or object. In this relationship, each and every object is independent from each other. For example, we can consider a car and its different wheels, the same wheel we can install into another car then it’s working file without any problem. We know that a car without wheels is not useful, so that is the reason we assemble all parts of the object, which is the aggregation relationship.

Examples of Composition and Aggregation in Java

Given below are the examples of composition and aggregation relationship:

Example #1: Composition

Code:

import java.io.*;
import java.util.*;
class Lib
{
public String name_book;
public String bk_author;
Lib(String name_book, String bk_author)
{
this.name_book = name_book;
this.bk_author = bk_author;
}
}
class Library
{
private final List<Lib> Lib_books;
Library (List<Lib> Lib_books)
{
this.Lib_books =Lib_books;
}
public List<Lib> book_details(){
return Lib_books;
}
}
class composition
{
public static void main (String[] args)
{
Lib book1 = new Lib("Database management", "Rajiv Chopra ");
Lib book2 = new Lib("MySql", "Rajiv Chopra l");
Lib book3 = new Lib("oracle", "Donald Burleson");
List<Lib> Lib_books = new ArrayList<Lib>();
Lib_books.add(book1);
Lib_books.add(book2);
Lib_books.add(book3);
Library library = new Library(Lib_books);
List<Lib> bks = library.book_details();
for(Lib bk : bks){
System.out.println("name_book : " + bk.name_book + " and "
+" bk_author : " + bk.bk_author);
}
}
}

Explanation:

  • In the above example, we try to implement composition relationships in java.
  • The final output of the above program we illustrate by using the following snapshot.

Output:

Composition and Aggregation in Java

Example #2: Aggregation Relationship

Code:

import java.io.*;
import java.util.*;
class stud_class
{
String stud_name;
int roll_no ;
String stud_dept;
stud_class(String stud_name, int roll_no, String stud_dept)
{
this.stud_name = stud_name;
this.roll_no = roll_no;
this.stud_dept = stud_dept;
}
}
class Depofcollege
{
String stud_name;
private List<stud_class> students;
Depofcollege(String stud_name, List<stud_class> students)
{
this.stud_name = stud_name;
this.students = students;
}
public List<stud_class> getStudentsDetails()
{
return students;
}
}
class college
{
String collegeName;
private List<Depofcollege> departments;
college(String collegeName, List<Depofcollege> departments)
{
this.collegeName = collegeName;
this.departments = departments;
}
public int totalstudents()
{
int noOfStudents = 0;
List<stud_class> students;
for(Depofcollege dept : departments)
{
students = dept.getStudentsDetails();
for(stud_class s : students)
{
noOfStudents++;
}
}
return noOfStudents;
}
}
class aggregation
{
public static void main (String[] args)
{
stud_class stud1 = new stud_class("Sameer", 5, "IT");
stud_class stud2 = new stud_class("Pooja", 6, "IT");
stud_class stud3 = new stud_class("Sanddep", 8, "Mech");
stud_class stud4 = new stud_class("Jenny", 2, "Mech");
List <stud_class> i_students = new ArrayList<stud_class>();
i_students.add(stud1);
i_students.add(stud2);
List <stud_class> me_students = new ArrayList<stud_class>();
me_students.add(stud3);
me_students.add(stud4);
Depofcollege IT = new Depofcollege("IT", i_students);
Depofcollege Mech = new Depofcollege("Mech", me_students);
List <Depofcollege> departments = new ArrayList<Depofcollege>();
departments.add(IT);
departments.add(Mech);
college college= new college("MIT", departments);
System.out.print("Count of students: ");
System.out.print(college.totalstudents());
}
}

Explanation:

  • In the above example, we try to implement an aggregation relationship in java.
  • The final output of the above program we illustrate by using the following snapshot.

Output:

Composition and Aggregation in Java

Conclusion

We have seen the basic syntax of composition and aggregation from the above article, and we also saw different examples of composition and aggregation. From this article, we have seen how and when we use the composition and aggregation in java.

The above is the detailed content of Composition and Aggregation in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java LiteralsNext article:Java Literals