Composition in Java

Last Updated : 13 May, 2026

Composition in Java is a design technique used to establish a strong has-a relationship between classes. In composition, one class contains another class, and the contained object cannot exist independently of the container object. It improves flexibility and code reusability by allowing classes to work together instead of relying on inheritance.

  • Represents a strong dependency between objects.
  • Objects are created and managed by the owner class.
  • Provides better maintainability and flexibility in applications.

Composition is preferred over inheritance in many cases because it provides better flexibility, code reusability, and maintainability.

Features

Composition provides a flexible way to reuse code without using inheritance. It improves maintainability, testability, and allows changing the behavior of a program dynamically at runtime.

  • Supports code reusability and flexibility.
  • Helps achieve multiple inheritance-like behavior in Java.
  • Improves testability and maintainability of classes.
  • Allows replacing or modifying member objects easily.
  • Enables dynamic behavior changes at runtime.

Real-Life Example: Library System

A real-world example of composition is a Library and Books relationship. The Library class manages and contains Book objects internally. Since the books are controlled by the library object and do not exist independently in this design, the relationship between them represents composition in Java.

Implementation:

Java
import java.util.*;

// Book class
class Book {

    // Data members
    private String title;
    private String author;

    // Constructor
    public Book(String title, String author)
    {
        this.title = title;
        this.author = author;
    }

    // Method to display book details
    public void displayBook()
    {
        System.out.println("Title : " + title
                           + " | Author : " + author);
    }
}

// Library class
class Library {

    // Library contains books
    private final List<Book> books = new ArrayList<>();

    // Method to add books
    public void addBook(String title, String author)
    {
        books.add(new Book(title, author));
    }

    // Method to display books
    public void showBooks()
    {
        for (Book book : books) {
            book.displayBook();
        }
    }
}

// Main class
class GFG {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating library object
        Library library = new Library();

        // Adding books through library
        library.addBook("Effective Java", "Joshua Bloch");

        library.addBook("Thinking in Java", "Bruce Eckel");

        library.addBook("Java: The Complete Reference",
                        "Herbert Schildt");

        // Displaying all books
        library.showBooks();
    }
}

Output
Title : Effective Java | Author : Joshua Bloch
Title : Thinking in Java | Author : Bruce Eckel
Title : Java: The Complete Reference | Author : Herbert Schildt

Explanation:

  • Book objects are created inside the Library class.
  • The Library class fully manages the lifecycle of books.
  • Books are dependent on the library object.
  • External classes cannot directly manage the internal book collection Hence, it represents a proper composition relationship.
Comment