What is Composition in Java With Examples

Java Composition is a fundamental concept in Object-Oriented Programming (OOP) that allows you to create complex objects by combining simpler ones.

In Java, composition is achieved through the use of instance variables that refer to other objects.

This relationship is known as an “has-a” relationship and is a way of establishing a more complex object from simpler objects.

Java composition is a powerful tool for designing complex systems, as it allows for more flexible and modular code.

This, in turn, makes the code easier to maintain and less prone to bugs.

In this article, we will take a closer look at Java Composition, including the definition, benefits, and code examples.


Definition of Java Composition

Java Composition is a relationship between two classes in which an instance of one class holds a reference to an instance of another class.

The class that holds the reference is referred to as the “composite class,” while the class being referred to is referred to as the “component class.”

For example, consider a car. A car has several components, such as wheels, an engine, and a transmission.

In Java, you can model this relationship by creating a “Car” class that holds references to instances of “Wheel,” “Engine,” and “Transmission” classes.

Benefits of Java Composition

Reusability

One of the main benefits of Java Composition is that it allows for code reuse.

By creating component classes, you can reuse them in other composite classes, reducing the amount of code you need to write and making your code more maintainable.

Flexibility

Another benefit of Java Composition is that it allows you to easily change the behavior of a composite class by changing the behavior of its component classes.

This can be done by creating different implementations of the component classes or by changing the component classes themselves.

Modularity

Java Composition also allows you to design your code in a more modular way.

This makes your code easier to understand and maintain, as you can work on one component at a time, without having to understand the entire system.

Code Examples

Now that we’ve covered the definition and benefits of Java Composition, let’s take a look at some code examples.

Example 1: Car and Engine

public class Car {
  private Engine engine;
  //other car properties and methods
  
  public Car(Engine engine) {
    this.engine = engine;
  }
  
  public void startEngine() {
    engine.start();
  }
}

public class Engine {
  public void start() {
    //start the engine
  }
}

In this example, the Car class holds a reference to an instance of the Engine class.

When the startEngine method is called, it delegates the work to the Engine class.

Example 2: House and Room

public class House {
  private List<Room> rooms;
  //other house properties and methods
  
  public House(List<Room> rooms) {
    this.rooms = rooms;
  }
  
  public int totalArea() {
    int total = 0;
    for (Room room : rooms) {
      total += room.area();
    }
    return total;
  }
}

public class Room {
  private int length;
  private int width;
  //other room properties and methods
  
  public Room(int length, int width) {
this.length = length;
this.width = width;
}

public int area() {
return length * width;
}
}

In this example, the House class holds a reference to a list of instances of the Room class.

The totalArea method calculates the total area of all rooms in the house by iterating over the list of rooms and calling the area method on each one.


Conclusion

Java Composition is a powerful concept in Object-Oriented Programming that allows you to create complex objects by combining simpler ones.

It provides benefits such as code reuse, flexibility, and modularity, making your code easier to understand and maintain.

By understanding and utilizing Java Composition in your code, you can design more flexible and scalable systems that are easy to maintain and less prone to bugs.

We hope this blog post has provided a clear overview of Java Composition and has shown you some code examples to help illustrate the concept.

If you have any questions or comments, please let us know in the comments section below.