Java composition with example

In one word, composition is nothing but a ‘Has-A’ relationship. As we know, in object oriented programming there are two popular relationship present, one is ‘Is A’ and another one is ‘Has A’ relationship. The main benefit of has a relationship is that this can reference many classes. Let us check an example of Television. We all know that Television has Monitor, Remote and Set Top Box. So, we can create a TV class as below:-

public class TV {

    //My TV has settop box
    //My TV has Monitor
    //My TV has remote

    //Now Monitor has  resolution(Resolution will be hd or non hd depending on input high or low) and Screen size also depending on input given by user
    // and on/off button.

    //Remote has one, off and channel change switch

    //Settop box has one/off button

  //So when I will build my TV, it has remote , by which I can on / off tv and also make the channel change. Or we can button on Monitor to on and off it.

private Monitor monitor;
private SetTopBox setTopBox;
private Remote remote;

    public TV(Monitor monitor, SetTopBox setTopBox, Remote remote) {
        this.monitor = monitor;
        this.setTopBox = setTopBox;
        this.remote = remote;
    }

    public Monitor getMonitor() {
        return monitor;
    }

    public void setMonitor(Monitor monitor) {
        this.monitor = monitor;
    }

    public SetTopBox getSetTopBox() {
        return setTopBox;
    }

    public void setSetTopBox(SetTopBox setTopBox) {
        this.setTopBox = setTopBox;
    }

    public Remote getRemote() {
        return remote;
    }

    public void setRemote(Remote remote) {
        this.remote = remote;
    }
}

In the above case all Monitor, Remote and the Set Top box are the different classes. In TV class we have used them, this is called the Has a relationship. Check the rest of the codes.

public class Monitor {

    public String model;
    public TVDimension dimension;
    public TVresolution resolution;

    public Monitor(String model, TVDimension dimension, TVresolution resolution) {
        this.model = model;
        this.dimension = dimension;
        this.resolution = resolution;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public TVDimension getDimension() {
        return dimension;
    }

    public void setDimension(TVDimension dimension) {
        this.dimension = dimension;
    }

    public TVresolution getResolution() {
        return resolution;
    }

    public void setResolution(TVresolution resolution) {
        this.resolution = resolution;
    }

   public  void MonitorOnButton()
   {
       System.out.println("Pressed  the on button of the monitor and it is loading");
       System.out.println("Your Monitor is of size " + dimension.getSize() + " inch" + " and having " +resolution.getResolution() + " resolution." );
   }

    public  void MonitoroffButton()
    {
        System.out.println("Pressed  the on button of the monitor and it is loading");
    }



}

This above monitor class also has a resolution and Dimension. So these two classes can be represented as below:-

public class TVDimension {

    private int size;

    public TVDimension(int size) {
        this.size = size;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void bigorsmallMonitor()
    {
        if(size>29)
        {
            System.out.println("big tv");
        }

        else {
            System.out.println("Small TV");
        }


    }


}
public class TVresolution {

   private String resolution;

    public String getResolution() {
        return resolution;
    }

    public void setResolution(String resolution) {
        this.resolution = resolution;
    }

    public TVresolution(String resolution) {
        this.resolution = resolution;
    }

    public void Resolutiontype()
    {
        if (resolution=="high")
            System.out.println("High resolution TV");

        else
            System.out.println("Low resolution TV");


    }
}

Now check the remote class:-

public class Remote {
    private String sizeOfRemote;
    private String modelOfRemote;
    private String colorOfRemote;

    public Remote(String sizeOfRemote, String modelOfRemote, String colorOfRemote) {
        this.sizeOfRemote = sizeOfRemote;
        this.modelOfRemote = modelOfRemote;
        this.colorOfRemote = colorOfRemote;
    }

    public String getSizeOfRemote() {
        return sizeOfRemote;
    }

    public void setSizeOfRemote(String sizeOfRemote) {
        this.sizeOfRemote = sizeOfRemote;
    }

    public String getModelOfRemote() {
        return modelOfRemote;
    }

    public void setModelOfRemote(String modelOfRemote) {
        this.modelOfRemote = modelOfRemote;
    }

    public String getColorOfRemote() {
        return colorOfRemote;
    }

    public void setColorOfRemote(String colorOfRemote) {
        this.colorOfRemote = colorOfRemote;
    }


    public void powerOnButton()
    {
        System.out.println("Power on button pressed for remote");
    }

    public void powerOffButton()
    {
        System.out.println("Power off button pressed for remote");
    }

    public void channelChangeButton()
    {
        System.out.println("Channel change button pressed for remote");
    }

}

Now the Set top box class also has another class as an attribute. So check Set top box along with that class:-

public class SetTopBox {
  private String model;
  private DimensionSetTopBox dimension;

    public SetTopBox(String model, DimensionSetTopBox dimension) {
        this.model = model;
        this.dimension = dimension;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public DimensionSetTopBox getDimension() {
        return dimension;
    }

    public void setDimension(DimensionSetTopBox dimension) {
        this.dimension = dimension;
    }


    public void setTopBoxOnSwitch()
    {
        System.out.println("Set top box is starting");

    }


    public void setTopBoxOffSwitch()
    {
        System.out.println("Set top box is stopping");

    }


}
public class DimensionSetTopBox {

 private int width;
 private int height;

    public DimensionSetTopBox(int width, int height) {
        this.width = width;
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

Now at last in the main class we will use all the references of these objects and will pass the appropriate values for the output:-

public class TVAPP {

public static void main(String[] args) {

//switch on monitor
Monitor monitor = new Monitor("Sony",new TVDimension(22),new TVresolution("high"));
SetTopBox setTopBox = new SetTopBox("Tatasky",new DimensionSetTopBox(22,44));
Remote remote = new Remote("big","tatasky","black");

TV tv = new TV(monitor,setTopBox,remote);

tv.getMonitor().MonitorOnButton();

//Swith on settop box

tv.getSetTopBox().setTopBoxOnSwitch();
tv.getSetTopBox().setTopBoxOffSwitch();
System.out.println("Your set top box name is "+ tv.getSetTopBox().getModel());


}
}

So in this above code blocks I have explained the composition of Java. Please copy these code blocks and paste at your own editor and change it according to your requirements.