Core Java Concepts and Syntax

What is Composition in Java?

What is Composition in Java?

Introduction

Composition is a connection that depicts a relationship between a component and a whole in which a part cannot exist without a whole. Java uses the composition design method to implement a connection.

Implementation of Composition in Java

Composition allows us to reuse code in the same way that Java inheritance does. An instance variable that makes references to other objects is used to create the composition. To put it another way, composition refers to a reference between two or more classes that use instance variables. In contrast to an "is-a" relationship, which is inherited, we also refer to composition as a "has-a" relationship frequently.

For instance, a classroom has a blackboard, or to say it another way, a classroom has a blackboard. Hence, the board "belongs to" the classroom.

Why composition over inheritance?

There is a considerable degree of flexibility in composition. Private member objects of the new class are normally unavailable to client programmers that use the class. This enables us to alter those elements without affecting the client code that is already in place.

Additionally, you may alter the member objects while your program is running to modify its behavior on the fly. This flexibility is not possible with inheritance since classes constructed using inheritance must be subject to compile-time limitations.

Benefits of composition

Composition is important because it allows us to reuse only the parts we actually need while controlling which additional objects are visible to client classes. When necessary, composition enables the construction of back-end classes.

Here is an example of composition in Java:

Code

// Java program to Illustrate Concept of Composition


// Importing required classes

import java.io.*;

import java.util.*;


// Class 1

// Helper class

// Book class

class Book {


// Member variables of this class

public String title;

public String author;


// Constructor of this class

Book(String title, String author)

{


// This keyword refers top current instance

this.title = title;

this.author = author;

}

}


// Class 2

// Helper class

// Library class contains list of books.

class Library {


// Reference to the list of books.

private final List<Book> books;


// Constructor of this class

Library(List<Book> books)

{


// This keyword refers to current instance 

this.books = books;

}


// Method of this class

// Getting the list of books

public List<Book> getListOfBooksInLibrary()

{

return books;

}

}


// Class 3

// Main class

class Infinity_board {


// Main driver method

public static void main(String[] args)

{


// Creating the objects of first class 

// inside main() method

Book b1

= new Book("EffectiveJ Java", "Infinity");

Book b2

= new Book("Thought in Java", "Board");

Book b3 = new Book("Java: Best website",

"Board Infinity");


// Creating the list of

// no. of books.

List<Book> book = new ArrayList<Book>();


// Adding books to List object

// using standard add() method

book.add(b1);

book.add(b2);

book.add(b3);


// Creating an object of second class

Library library = new Library(book);

// Calling method of class 2 and storing list of

// books in List Here List is declared of type

// Books(user-defined)

List<Book> books

= library.getListOfBooksInLibrary();

// Iterating over for each loop

for (Book bk : books) {


// Print and display the title and author of

write your code here: Coding Playground

OUTPUT