Composition In Java With Example

We have discussed the concept of inheritance which is a powerful mechanism of reusing code, minimizing data redundancy, and improving the organization of object oriented system. Inheritance is suitable only when classes are in a relationship in which child class is a parent class. For example: A Car is a Vehicle, so the class Car has all the features or properties of class Vehicle and in addition to its own features. However, we cannot always have is a relationship between objects of different classes. Let us say with example: A car is not a kind of engine. To represent such a relationship, we have an alternative to inheritance known as composition. It is applied when classes are in a relationship in which child class has a parent class.

Unlike Inheritance in which a subclass extends the functionality of a superclass, in composition, a class reuses the functionality by creating a reference to the object of the class it wants to reuse. For example: A car has a engine, a window has a button, a zoo has a tiger.

Composition is a special case of aggregation. In other words, a restricted aggregation is called composition. When an object contains the other object and the contained object cannot exist without the other object, then it is called composition.

Program Example of Composition:

Let us consider the following program that demonstrates the concept of composition.

Step 1: First we create a class Bike in which we declare and define data members and methods:

class Bike
{

  // declaring data members and methods

  private String color;

  private int wheels;

  public void bikeFeatures()

  {

    System.out.println("Bike Color= "+color + " wheels= " + wheels);

  }

  public void setColor(String color)

  {

    this.color = color;

  }

  public void setwheels(int wheels)

  {

    this.wheels = wheels;

  }

}

Step 2: Second we create a class Honda which extends the above class Bike. Here Honda class uses HondaEngine class object start() method via composition. Now we can say that Honda class HAS-A HondaEngine:

class Honda extends Bike

{

  //inherits all properties of bike class

  public void setStart()

  {

    HondaEngine e = new HondaEngine();

    e.start();

  }

}

Step 3: Third we create a class HondaEngine through which we uses this class object in above class Honda:

class HondaEngine

{

  public void start()

  {

    System.out.println("Engine has been started.");

  }

  public void stop()

  {

    System.out.println("Engine has been stopped.");

  }

}

Step 4: Fourth we create a class CompositionDemo in which we make an object of Honda class and initialized it:

class CompositionDemo

{

  public static void main(String[] args)

  {

    Honda h = new Honda();

    h.setColor("Black");

    h.setwheels(2);

    h.bikeFeatures();

    h.setStart();

  }

}

Output:

Bike color= Black   wheels= 2
Engine has been started

Importance of Composition:

  • In composition we can control the visibility of other object to client classes and reuse only what we need.
  • Composition allows creation of back-end class when it is needed.

Comparing Composition and Inheritance:

  • It is easier to change the class which is implementing composition than inheritance.
  • In inheritance, you cannot add method to a child class with the same method name but a different return type as that method inherited from a parent class. On the other hand, composition allows us to change the interface of a front-end class without affecting back-end classes.
  • Composition is done at run time i.e. dynamic binding while Inheritance is done at compile time i.e. static binding.
  • If you want to reuse code and there is no is-a relationship, then use composition. You don’t need to use inheritance for code reuse.
  • If you want polymorphism, but there is no is-a relationship, then use composition with interfaces. You don’t need to use inheritance to get polymorphism.

Leave a Reply

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