Javatpoint Logo

What is composition in java?

By: rajmsc*** On: Thu Mar 13 14:56:59 IST 2014     Question Reputation5 Answer Reputation0 Quiz Belt Series Points2  7Blank User
Hi Can any one explain what is composition in java?

Thanks in a Advance

Regards
Raj Kumar R
Up0Down

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

Java composition is achieved by using instance variables that refers to other objects.

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

}
Image Created0Down

By: [email protected] On: Thu Mar 13 16:14:01 IST 2014 Question Reputation0 Answer Reputation359 Belt Series Points0 359User Image
Are You Satisfied :3Yes0No
 
Composition is a Has-A relationship...means one class can use the functionality of another class using the object of that class...see below
class A
{ int x=10;}
class B
{ int y=20;
A a=new A();
System.out.println(a.x+" "+y);
}
its a sample example to understand composition...

Image Created0Down

By: [email protected] On: Mon Mar 17 00:43:25 IST 2014 Question Reputation5 Answer Reputation19 Belt Series Points0 24User Image
Are You Satisfied :0Yes2No
 
difference between composition and aggregation?Image Created0Down

By: [email protected] On: Wed Oct 14 12:19:04 IST 2015 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.
Example: A class contains students. A student cannot exist without a class. There exists composition between class and students.

Composition is more restrictive. When there is a composition between two objects, the composed object cannot exist without the other object. This restriction is not there in aggregation. Though one object can contain the other object, there is no condition that the composed object must exist. The existence of the composed object is entirely optional. In both aggregation and composition, direction is must. The direction specifies, which object contains the other object.

Example: A Library contains students and books. Relationship between library and student is aggregation. Relationship between library and book is composition. A student can exist without a library and therefore it is aggregation. A book cannot exist without a library and therefore its a composition. For easy understanding I am picking this example. Don?t go deeper into example and justify relationships!
Image Created0Down

By: [email protected] On: Wed Jan 27 17:49:14 IST 2016 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No