Composition in Java
Composition
Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the inheritance. In composition, we apply the "has-a" relationship to ensure that the code can be reused. Utilizing an instance variable that makes references to other objects, the composition is accomplished. It is referred to as composition when one item contains another and the enclosed object cannot exist without the other. To put it another way, composition refers to a relationship between two or more classes that contain instance variables, and an instance must be constructed before it is used.
When one element includes another and that other object is entirely dependent upon it, a composition relationship between the two objects is established. It should be impossible for the contained object to function without the parent object. To put it simply, we may say that it is a method for describing the relationship between two or more classes. And to do that, we make use of the instance variable, which must first be defined.
Key Points
- A component of a relationship is represented by the composition.
- In the composition, both things are connected to one another.
- When an item has a composed object and the composed object is dependent on another entity to function, this is when two entities are said to be composed.
- Give composition preference over inheritance.
- With an instance variable that makes references to other objects, the composition is done.
A narrower version of aggregation is the composition. It is impossible for one class to work without another one in composition since it depends on and contains another class.
Implementation
The Java programming language uses the keyword "final" to express composition. This is because that the 'Parent' object, that makes a component object 'final,' may assume it to be accessible and work.
Lets go through a small example to know how the syntax is written and understand the program flow
Syntax
public class Parent class name
{
private final Child class name child class object;
public Parent class name ()
{
child = new Child ();
}
}
class Child class name
{
private String type;
}
Main.java
public void BeginEngine (){
System.out.println(“The bike has Started.”);
}
public void TerminateEngine (){
System.out.println(“The bike has Stopped.”);
}
}
class MotorBike {
private String colour;
private int maxi_Speed;
public void bikeDetails(){
System.out.println(“Bike Colour= “+colour + “; Maximum Speed= ” + maxi_Speed);
}
//Setting colour of the bike
public void setColour(String colour) {
this.colour = colour;
}
//Setting maximum bike Speed
public void setMaxiSpeed(int maxi_Speed) {
this.maxi_Speed = maxi_Speed;
}
}
Class Yamaha extends Bike{
public void YamahaStart(){
BikeEngine Yamaha_Engine = new BikeEngine(); //composition
Yamaha_Engine.startEngine();
}
}
public class Main {
public static void main(String[] args) {
Yamaha YamahaR15 = new Yamaha();
YamahaR15.setColour(“Red”);
YamahaR15.setMaxSpeed(100);
YamahaR15.bikeDetails();
YamahaR15.YamahaStart();
}
}
Output:
Bike colour = Red; Maximum speed = 100
The bike has started
The result, which displays the specifications of the Yamaha R15 motorcycle, is generated using composition.
Advantages
The below advantages can be gained by using Java's composition technique:
- It is very convenient to choose object composition over the class inheritance. Code reusability and polymorphism can also be achieved using composition technique.
- The composition is adjustable, allowing you to modify the function of each object and update the class execution dynamically.
- In composition technique minimum number of classes are used in program code.
- The conceptually accurate relationship among classes is "HAS-A," not "IS-A."
- Improve working testing process provided by composition in Java is very helpful for test-driven development.
- By combining many objects as one, it is feasible to create "multiple inheritances" in programming languages like java.
- Neither function nor object name differences exist in composition.
Disadvantages
- The composition technique is not useful or not the solution in every scenario.
- Due to inheritance, almost any situation in which an instance of the base class would be appropriate can also be applied to an instance of the derived type. Composition restricts such behavior.
- Classes often derive from internal improvements that compositions at run-time do not.
- Only when an object is the parent of another, the composition make good sense. Just like when a human being owning a dog as a pet.