Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

How To Implement Java Composition In Depth?

Published on Aug 13,2019 5.7K Views


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

  • It represents a part-of relationship.
  • In composition, both the entities are dependent on each other.
  • When there is a composition between two entities, the composed object cannot exist without the other entity. For example, if order HAS-A line-items, then an order is a whole and line items are parts
  • If an order is deleted then all corresponding line items for that order should be deleted.
  •  Favor Composition over Inheritance.

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

Output- Java Composition - Edureka

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 14th December,2024

14th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Implement Java Composition In Depth?

edureka.co