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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (45 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (307 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How To Implement Java Composition In Depth?

Published on Aug 13,2019 44 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.