This blog will provide a pathway to applying “Composition” in Java.
What is Java Composition?
The “Composition” in Java utilizes a “has-a” relationship/association. The “has-a” relationships usually determine if a certain object has another object. The composition is carried out by utilizing an instance variable that corresponds to other objects. The composition relationship of two objects is done when one object accumulates another object, and that object is entirely dependent on it. It is such that the contained/accumulated object cannot exist without the existence of its parent/super object.
Demonstration
Let’s overview the following illustration to get a better understanding of the discussed concept:
In the above illustration, the class “BikeEngine” is a parent class. Also, “Yamaha” is a Bike that extends the class “Bike”. In addition to that, the “BikeEngine” class object is used in the “Yamaha” class.
Example: Applying “Composition” in Java
This example applies composition by maintaining a “has-a” relationship. The above illustration will be demonstrated in the below-stated code, thereby demonstrating the concept more clearly:
public void start(){
System.out.println("Engine Started!");
}}
class Bike {
public void specs(){
System.out.println("The Bike Model is -> 2023");
}
}
class Yamaha extends Bike{
public void compose(){
BikeEngine engine = new BikeEngine();
engine.start();
}}
public class Composition {
public static void main(String[] args) {
Yamaha object = new Yamaha();
object.specs();
object.compose();
}}
According to the above code lines, perform the following steps:
- Firstly, create a class named “BikeEngine”.
- In the class definition, define the function “start()” displaying the stated message.
- Likewise, declare another class named “Bike” comprising the function that displays the given message.
- Now, define the class “Yamaha” inheriting the class “Bike” via the “extends” keyword.
- In its(class) definition, specify a function that creates an object of the “BikeEngine” class using the “new” keyword and the “BikeEngine()” constructor.
- Note: Here, a “has-a” relationship is applied which can be related as -> (“Yamaha” bike has a “engine”) representing “Composition”.
- Also, invoke the defined function of the class i.e., “BikeEngine” whose object is created.
- In the “main()” method, create a “Yamaha” class object via the discussed approach.
- Lastly, access the inheriting class function and the function of the class itself(whose object is created), respectively.
Output
In this outcome, it can be implied that the “main()” method invokes the “Yamaha” class(in which the composition is applied and inherits the “Bike” class as well).
Conclusion
The “Composition” in Java is based on the “has-a” relationship and is carried out by utilizing an instance variable that corresponds to other objects. It is such that a class object contains another class object. This write-up demonstrated the usage and implementation of “Composition” in Java.