OOP (Object Oriented Programming) tries to logically represent the real world entities and their relationship. The entities get created when we create classes but identifying and defining the relationship between them is an art which requires practice to learn. There are many kind of relationships which can exist between real world entities and hence our classes in the application. In this tutorial, we shall how the uses-a and has-a relationships compare to each other and when should we use which one.

Differences between Association and Composition

As the literal meaning of these words makes it clear that uses-a relationship exists between two entities when one makes use of the other for performing some task but has-a relationship exists when one entity contains the other. The essential difference between the two is that while in case of has-a relationship, the contained object is destroyed if the container gets destroyed and this is not the case with uses-a relationship.

Let us understand the difference with an example and see the code for the same later. A Man can have a Pen for writing however the Pen can be transferred from him to another Man. If a man doesn’t exist then it doesn’t mean that the Pen will also cease to exist. On the other hand, A Man has Brain and this relationship is whole part relationship where if the Man ceases to exist, the Brain will also cease to exist. As we can see that both Pen and Brain are used by Man for performing some tasks or job but the relationship Man holds with Pen and Brain is of type uses-a and has-a respectively.

Note that uses-a is also known as Association OOPS relationship and has-a is known as Composition OOPS relationship.

Association relationship code example

Now coming the code example for Association relationship. In the following code the same pen instance is used by two men. As we can see that the same pen object is transferred from one Man object to another even when the first Man instance is destroyed.

Man.java

package com.example;

public class Man {

	Pen p;
	
	public void setPen(Pen pen) {
		p = pen;
	}
	
	public void write() {
		p.write("hello");
	}
}

Pen.java

package com.example;

public class Pen {

	public void write(String str) {
		System.out.println(str);
	}
}

Main.java

package com.example;

public class Main {

	public static void main(String[] args) {
		Man m1 = new Man();
		Man m2 = new Man();
		Pen p = new Pen();
		
		m1.setPen(p);
		m1.write();
		
		m1= null;
		
		m2.setPen(p);
		m2.write();
		
	}
}

Composition relationship code example

Similar to the above code, we can also write code for composition relationship as shown below:

Man.java

package com.example;

public class Man {

	Brain brain = new Brain();
	
	public void think() {
		brain.think("thinking");
	}
}

Brain.java

package com.example;

public class Brain {

	public void think(String str) {
		System.out.println(str);
	}
}

Main.java

package com.example;

public class Main {

	public static void main(String[] args) {
		Man m1 = new Man();
		Man m2 = new Man();
		
		m1.think();
	
		m1= null;
		
		m2.think();
		
	}
}

In the above code, the difference from association relationship is that the Brain is a member instance variable of Man and is not injected by someone else. By making it member reference variable, we are making the Brain instance is being fully dependent on Man. If Man instances ceases to exist, the Brain instance associated with also ceases to exist. Also note that a single Brain object can’t be shared among two men.

Related Posts

Uses-A and Has-A relationship in OOPS admin Core Java
OOP (Object Oriented Programming) tries to logically represent the real world entities and their relationship. The entities get created when we create classes but identifying and defining the relationship between them is an art which requires practice to learn. There are many kind of relationships which can exist between...
<strong>OOP (Object Oriented Programming)</strong> tries to logically represent the real world entities and their relationship. The entities get created when we create classes but identifying and defining the relationship between them is an art which requires practice to learn. There are many kind of relationships which can exist between real world entities and hence our classes in the application. In this tutorial, we shall how the <strong>uses-a and has-a relationships</strong> compare to each other and when should we use which one. <h2>Differences between Association and Composition</h2> As the literal meaning of these words makes it clear that uses-a relationship exists between two entities when one makes use of the other for performing some task but has-a relationship exists when one entity contains the other. The essential difference between the two is that while in case of has-a relationship, the contained object is destroyed if the container gets destroyed and this is not the case with uses-a relationship. Let us understand the difference with an example and see the code for the same later. A Man can have a Pen for writing however the Pen can be transferred from him to another Man. If a man doesn't exist then it doesn't mean that the Pen will also cease to exist. On the other hand, A Man has Brain and this relationship is whole part relationship where if the Man ceases to exist, the Brain will also cease to exist. As we can see that both Pen and Brain are used by Man for performing some tasks or job but the relationship Man holds with Pen and Brain is of type uses-a and has-a respectively. Note that uses-a is also known as Association <a href="http://www.javaexperience.com/solid-oops-principle/" title="SOLID OOPS Principle">OOPS</a> relationship and has-a is known as Composition OOPS relationship. <h2>Association relationship code example</h2> Now coming the code example for Association relationship. In the following code the same pen instance is used by two men. As we can see that the same pen object is transferred from one Man object to another even when the first Man instance is destroyed. <h3>Man.java</h3> 1 <h3>Pen.java</h3> 1 <h3>Main.java</h3> 1 <h2>Composition relationship code example</h2> Similar to the above code, we can also write code for composition relationship as shown below: <h3>Man.java</h3> 1 <h3>Brain.java</h3> 1 <h3>Main.java</h3> 1 In the above code, the difference from association relationship is that the Brain is a member instance variable of Man and is not injected by someone else. By making it member reference variable, we are making the Brain instance is being fully dependent on Man. If Man instances ceases to exist, the Brain instance associated with also ceases to exist. Also note that a single Brain object can't be shared among two men.
Difference between CodePoint and CodeUnit in Java Strings
Interrupting threads in Java
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments