What is Composition in java

What is Composition in java?

Generally, when we talk about composition we are referring to the ingredients of a substance. Or, we can say that the substance is composed of the following ingredients. But in programming, it has its own meanings. When we say what is composition in java or any other programming language we are actually referring to “has-a” relation. Now the question that arises in our mind is what do we mean by has-a relation?  The answer to this question is very simple. I.e. A class has objects of other classes as its members. In composition, we can create a new class by using the objects of the previously defined classes. So, Composition in java means that we represent the has-a relationship between the classes. I.e. An object of one class is present as a member of another class.

Let us say that we have a class named as AlarmClock and this class is supposed to ring the alarm. And for that, it needs to know the time. We can achieve this by referencing the Time class to it. I.e. We will create an object of Time class as a member in AlarmClock. This is aggregation between classes allowing the AlarmClock to use the functionality of Time class.

Composition is a special form of aggregation. In composition, the owned class can only exist if the owning class exits. It is more restrictive than aggregation. Composition is also different from Inheritance in which we have “is-a” relation.

For example, we have a class wall and another class named as the window. Since a window can exist only if the wall class exists. So, the wall class has reference to the window class.  We can achieve this by creating the object of window class inside the wall class. This is the composition between the classes.

Cons and pros of Composition in Java

  •   The very first and the main advantage of the composition is code reusability.
  •   Controlled visibility of other objects to the client class.
  •   No conflict of methods or properties of classes.
  •   In composition, the code is a bit difficult to understand.
  •   The design using composition will have less number of classes but more number of objects.


Composition in java example

Some other real-life examples of the composition are:

An educational institute has departments. And departments strongly depend on the institute. As the departments can only exit if the institute exits.

Student and teachers have attributes of the person and school has teachers and students. Without the existence of school students and teachers can’t exist. They still exist as a person (aggregation) but they are not students or a teacher without a school(composition).

A person has a job. Since the job independently has no value unless someone is doing that job.

A sample program is :

package example;
public class Job {
       private String title;
       private long pay;
       public long getPay() {
              return pay;
       public void setPay(long pay) {
              this.pay = pay;
       public String getTitle() {
              return title;
       public void setTitle(String title) {
              this.title = title;

Now we have a person class which holds the job class object.

package example;
public class Person {
       private Job job;

              this.job =  new Job();
              job.setTitle("Software Engineer");
       public String getTitle(){
              return job.getTitle();
       public long getPay(){
              return job.getPay();

Below is the class to access the person class.

package example;
public class RunPerson {
       public static void main(String args[]) {
              Person p = new Person();
              System.out.print("Title of the Job is " + p.getTitle() + " with the pay = " + p.getPay());

The Output for the above class is:

Title of the Job is Software Engineer with the pay = 50000

Things we should focus in the above example are:

  • The Person class has a reference to Job class by declaring a private instance of Job class.
  • And the job class object is created inside the only constructor of the Person class.
  • This leads to a situation where whenever we create a new object for Person class it has a reference to Job class as well.
  • This is a very interesting situation in which we cannot set Job from outside.
  • So the existence of Job is completely dependent on Person.
  • Which elaborates the composition as a strong form of association in which the owned object cannot exist without the existence of the owning object.


Let’s have a look at composition in java example of library. We have a library class and a book class. As we know that the library has books. Sample program for the modeling of this example is:

Book class:

package example;
public class Book {
       private String title;
       private String author;
       private double price;
       private double edition;
       private String category;

       public String getTitle() {
              return title;
       public void setTitle(String title) {
              this.title = title;
       public String getAuthor() {
              return author;
       public void setAuthor(String author) {
              this.author = author;
       public double getPrice() {
              return price;
       public void setPrice(double price) {
              this.price = price;
       public double getEdition() {
              return edition;
       public void setEdition(double edition) {
              this.edition = edition;
       public String getCategory() {
              return category;
       public void setCategory(String category) {
              this.category = category;

Library class:

package example;

import java.util.ArrayList;
import java.util.List;

public class Library {
       private static List<Book> books;
       private Book b;
       Library() {
              books = new ArrayList<Book>();
       public void addBook(String title, String author, String category, double price, double edition) {

              // adding the details of the book.
              b = new Book();

              // adding a book to the library.
       public void getTotalBooks() {
              for (Book book : books) {
                     System.out.println("Title: " + book.getTitle());
                     System.out.println("Author: " + book.getAuthor());
                     System.out.println("Category: " + book.getCategory());
                     System.out.println("Price: " + book.getPrice());
                     System.out.println("Edition: " + book.getEdition());

To run the above example we use this simple class.

package example;
public class RunLibrary {
       public static void main(String args[]) {
       Library lib = new Library();    
       lib.addBook("Java 2: The Complete Reference","Herbert Schildt", "Programming", 35.70 , 9 );
       lib.addBook("The Girl Who Lived","Christopher Greyson", "Novel", 14.39 , 1 );
       lib.addBook("Shahab Nama","Qudrat-Ullah-Shahab", "Autobiography", 14.50 , 9 );


what is composition in java example

Here you can see that the existence of books is completely dependent on the library.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.