How To Implement Java Composition In Depth?

Published on Aug 13,2019 5.7K Views

How To Implement Java Composition In Depth?

edureka.co

This article will introduce you yet another interesting programming concept, that is Java Composition and follow it up with a programmatic demonstration. Following pointers will be covered in this article,

Composition is an association represents a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted. It has a stronger relationship.

Moving on with this article on Java Composition

Key Points

Moving on with this article on Java Composition

Benefits

If you are looking for code reuse and the relationship between two classes is has-a then you should use composition rather than inheritance. Benefit of using composition in java is that we can control the visibility of other object to client classes and reuse only what we need. Also if there is any change in the other class implementation, for example getSalary returning String, we need to change Person class to accommodate it but client classes doesn’t need to change. Composition allows creation of back-end class when it’s needed, for example we can change PersongetSalary method to initialize the Job object at runtime when required.

Moving on with this article on Java Composition

Program Example of Java 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();
}
}

Next step in this Java Composition program is

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.");
}
}

Final step of this Java Composition Program

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();
}
}

Thus we have come to an end of this article on ‘Java Composition’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework