Javatpoint Logo

simple composition example program in java

By: narasi*** On: Wed Jul 31 23:04:01 EDT 2013     Question Reputation1 Answer Reputation0 Quiz Belt Series Points0  1Blank User
HI javatpoint team please can help any one how to implement composition...Up0Down

 
Composition is the design technique to implement has-a relationship in classes. We can use java inheritance or Object composition for code reuse.

composition in java is achieved by using instance variables that refers to other objects. For example, a Person has a Job. Let?s see this with a simple code.

Job.java

package com.journaldev.composition;

public class Job {
private String role;
private long salary;
private int id;

public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}


}


Person.java

package com.journaldev.composition;


public class Person {

//composition has-a relationship
private Job job;

public Person(){
this.job=new Job();
job.setSalary(1000L);
}
public long getSalary() {
return job.getSalary();
}

}
Here is a test class that uses person object and get it?s salary.

TestPerson.java


package com.journaldev.composition;

public class TestPerson {

public static void main(String[] args) {
Person person = new Person();
long salary = person.getSalary();
}

}
Notice that above test program is not affected by any change in the Job object. 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 is that we can control the visibility of other object to client classes and reuse only what we need.
Image Created0Down

By: [email protected] On: Thu Aug 01 06:55:46 EDT 2013 Question Reputation0 Answer Reputation392 Belt Series Points0 392User Image
Are You Satisfied :2Yes2No
 
Composition

As you progress in an object-oriented design, you will likely encounter objects in the problem domain that contain other objects. In this situation you will be drawn to modeling a similar arrangement in the design of your solution. In an object-oriented design of a Java program, the way in which you model objects that contain other objects is with composition, the act of composing a class out of references to other objects. With composition, references to the constituent objects become fields of the containing object.

For example, it might be useful if the coffee cup object of your program could contain coffee. Coffee itself could be a distinct class, which your program could instantiate. You would award coffee with a type if it exhibits behavior that is important to your solution. Perhaps it will swirl one way or another when stirred, keep track of a temperature that changes over time, or keep track of the proportions of coffee and any additives such as cream and sugar.

To use composition in Java, you use instance variables of one object to hold references to other objects. For the CoffeeCup example, you could create a field for coffee within the definition of class CoffeeCup, as shown below: [bv: implement the methods]

// In Source Packet in file inherit/ex1/CoffeeCup.java
class CoffeeCup {

private Coffee innerCoffee;

public void addCoffee(Coffee newCoffee) {
// no implementation yet
}

public Coffee releaseOneSip(int sipSize) {
// no implementation yet
// (need a return so it will compile)
return null;
}

public Coffee spillEntireContents() {
// no implementation yet
// (need a return so it will compile)
return null;
}
}

// In Source Packet in file inherit/ex1/Coffee.java
public class Coffee {

private int mlCoffee;

public void add(int amount) {
// No implementation yet
}

public int remove(int amount) {
// No implementation yet
// (return 0 so it will compile)
return 0;
}

public int removeAll() {
// No implementation yet
// (return 0 so it will compile)
return 0;
}
}
In the example above, the CoffeeCup class contains a reference to one other object, an object of type Coffee. Class Coffee is defined is a separate source file.

The relationship modeled by composition is often referred to as the "has-a" relationship. In this case a CoffeeCup has Coffee. As you can see from this example, the has-a relationship doesn't mean that the containing object must have a constituent object at all times, but that the containing object may have a constituent object at some time. Therefore the CoffeeCup may at some time contain Coffee, but it need not contain Coffee all the time. (When a CoffeeCup object doesn't contain Coffee, its innerCoffee field is null.) In addition, note that the object contained can change throughout the course of the containing object's life.
Image Created0Down

By: [email protected] On: Mon Oct 05 18:35:20 IST 2015 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No