Updated April 5, 2023
Introduction to Composition in OOPS
Composition in OOPS is a fundamental concept and describes a class that refers to one or more objects of the other class instance variables. It drives the design of the application and lets us know how the application should evolve as there are new features getting added or any requirements change. It is also one of the specialized but restricted forms of aggregation where two entities are dependent on each other. In Java, Object-Oriented Programming, it implements has-a relationship, which is achieved by using an instance variable referring to other objects.
Example:
An example of has-a relationship, i.e., Composition in OOPS.
- The class has a teacher
- Zoo has a lion
- The bike has an engine
It is one of the special types of aggregation; restricted aggregation is called composition. This scenario is known as Composition, when an object contains other objects, and the contained object can’t exist without other objects.
Syntax of Composition in OOPS
There is no particular syntax for composition, but it is pure programming of the concept.
Below is a sample of how composition can be written in Java:
Code:
public class Bike {
private Engine engine; // this shows has – a relationship. (Bike has a Engine)
public Bike() {
this.engine = new Engine();
engine.setCapacity(250L);
}
}
For all Composition examples, the user can control the visibility of other objects to other classes but reuse only what is needed. In addition, it allows back-end class creation when needed.
So by making use of has – a relationship among classes, known as Composition, we are making the program use instance of the class directly instead of extending it from other classes, similar to inheritance.
- It represents part of the relationship.
- Both entities are dependent on each other, i.e., Bike has – an engine.
- If Bike has – an engine, i.e., Entity1 has an Entity2, which means Entity1 is considered as a whole and Entity2 is part of Entity1. Hence composing objects can’t exist without other objects.
- If Bike Entity is deleted, the corresponding Entity2 for the Entity1 has to be deleted.
In OOPS, Composition is represented as below:
It has a stronger relationship, represents part of a relationship. For e.g., Hotel has a room. The hotel is a whole, and the room is a part of it. If Hotel is removed from the relationship, then all the corresponding rooms are to be deleted.
Examples of Composition in OOPS
Given below are the examples of Composition in OOPS:
Example #1
Composition in Java OOPS.
Code:
class Car
{
private String model;
private int wheelCount;
public void catFeatures()
{
System.out.println("Car Model= "+model + " wheels= " + wheelCount);
}
public void setModel(String model)
{
this.model = model;
}
public void setwheelCount(int wheelCount)
{
this.wheelCount = wheelCount;
}
}
class Maruti extends Car
{
public void setstartengine()
{
MarutiEngine me = new MarutiEngine();
me.startengine();
}
}
class MarutiEngine
{
public void startengine()
{
System.out.println("Maruti Engine starts!");
}
public void stopengine()
{
System.out.println("Maruti Engine stops!");
}
}
class Demo
{
public static void main(String[] args)
{
Maruti m = new Maruti();
m.setModel("Maruti Dzire 2020");
m.setwheelCount(4);
m.catFeatures();
m.setstartengine();
}
}
Output:
So here we have taken Car as an Entity and Model and Wheel as Entity 2. Which means Car has a Model and Car has a wheel.
Example #2
Song has audioname and singer.
Code:
import java.io.*;
import java.util.*;
class Audio {
public String audioName;
public String singer;
Audio(String audioName, String singer)
{
this.audioName = audioName;
this.singer = singer;
}
}
class Album {
private final List<Audio> songs;
Album(List<Audio> songs) { this.songs = songs; }
public List<Audio> getTotalSongsInAlbum()
{
return songs;
}
}
class Demo {
public static void main(String[] args)
{
Audio s1 = new Audio("Pehli Nazar mein", "AR Rehman");
Audio s2 = new Audio("Mallare", "Sid Sriram");
Audio s3 = new Audio("Aankhon mein teri", "DSP");
Audio s4 = new Audio("Wanne be Chamak Chalo O O", "Mahadevan");
List<Audio> song = new ArrayList<Audio>();
song.add(s1);
song.add(s2);
song.add(s3);
song.add(s4);
Album album = new Album(song);
List<Audio> songs = album.getTotalSongsInAlbum();
for (Audio sng : songs) {
System.out.println("Audio : " + sng.audioName
+ " and "
+ " Singer : " + sng.singer);
}
}
}
Output:
So here we have taken an example of album songs having a singer and audio name.
User finds it easier to change the class which is implementing composition. In OOPS, it is done at run time, which means dynamically binded.
If a user wants to reuse code and does not find is a relationship, it can be used. If a user wants polymorphism and does not find is a relationship, it can be used.
Conclusion
We have seen what Composition in OOPS means and how it is declared in Java. We have also seen some key points to be noted while implementing the concept. Have implemented few examples that are lengthy but still the simplest examples that will help understand the concept in a better way. It is nothing but Has – A relationship. You can get many examples with Has – A relationship and write code on your own.
Recommended Articles
We hope that this EDUCBA information on “Composition in OOPS” was beneficial to you. You can view EDUCBA’s recommended articles for more information.