The IS-A relationship refers to inheritance and HAS-A relationship refers to aggregation/composition in OOPS and UML.

IS-A or Inheritance

Inheritance is used for a parent-child and code reuse relationship. A concrete class, abstract class or an interface could be a super type of another class. The intention of such a relationship is code reuse. When such a relationship is held between two classes or interfaces, it is also termed as IS-A relationship. When looking at such a relationship, the literal meaning of IS-A comes into play. For example: An Apple IS-A fruit, A car is a vehicle and A Tablet is an electronic device.

Sample code showing how to create IS-A or inheritance in Java follows:

package com.example;

class Rest{}

public class Test extends Rest{
}

HAS-A or Composition

When an instance of one type is a member of another then the relationship between the two is known as aggregation or composition. To be more specific, the composition operation is a whole part relationship where the enclosing type fully manages the life-cycle of enclosed type. If the enclosing type is not managing the life-cycle of enclosed type then it is an aggregation relationship.

There is very thin difference between composition and aggregation relationships. A classical example is Car, Chassis and Tyre. While the relationship between Car and Chassis is composition, the relationship between CAR and tyre is of type aggregation. Note both these relationships fall under the category of association.

Sample code showing how to create HAS-A relationship in Java follows:

package com.example;

class Rest{}

public class Test{
Rest r = new Rest();
}

Choosing between the two

Choosing between is-a and has-a relationship depends upon the actual requirement. When the relationship has to a parent-child relationship than it is suitable to go for is-a relationship. But when the relationship involves a container and an entity then it is best to go for has-a relationship.
For example:
a car is a vehicle and has a gearbox. Here we should be having an inheritance relationship between car and vehicle class the the relationship between car and a gearbox will be of type composition.

Thus the differentiating part is that whether in real world the relationship between the two entities is of type container or parent-child. It is important to analyze the type of relationship before coding the application because if the changes are done at a later stage then it costs more to project.

UML Notation

In UML, the inheritance relationship easily an arrow followed by a blank arrowhead. On the other hand the composition relationship is an arrow followed by a filled diamond. In case of inheritance relationship the arrowhead needs to be pointing at the entity which is being inherited but in case of position relationship the arrowhead needs to be pointing at the contained object. Both these notations are shown below.

Inheritance:

inheritance and UML

inheritance and UML

Composition

composition and UML

IS-A and HAS-A relationship in OOPS and Java admin Core Java
The IS-A relationship refers to inheritance and HAS-A relationship refers to aggregation/composition in OOPS and UML. IS-A or Inheritance Inheritance is used for a parent-child and code reuse relationship. A concrete class, abstract class or an interface could be a super type of another class. The intention of such a...
The IS-A relationship refers to inheritance and HAS-A relationship refers to aggregation/composition in OOPS and UML. <h2>IS-A or Inheritance</h2> Inheritance is used for a parent-child and code reuse relationship. A concrete class, abstract class or an interface could be a super type of another class. The intention of such a <a href="http://www.javaexperience.com/inheriting-collections-to-create-custom-collections/" title="Inheriting Collections classes to make custom classes">relationship is code reuse</a>. When such a relationship is held between two classes or interfaces, it is also termed as IS-A relationship. When looking at such a relationship, the literal meaning of IS-A comes into play. For example: An Apple IS-A fruit, A car is a vehicle and A Tablet is an electronic device. Sample code showing how to create IS-A or inheritance in Java follows: 1 <h2>HAS-A or Composition</h2> When an instance of one type is a member of another then the relationship between the two is known as aggregation or composition. To be more specific, the composition operation is a whole part relationship where the enclosing type fully manages the life-cycle of enclosed type. If the enclosing type is not managing the life-cycle of enclosed type then it is an aggregation relationship. There is very thin difference between composition and aggregation relationships. A classical example is Car, Chassis and Tyre. While the relationship between Car and Chassis is composition, the relationship between CAR and tyre is of type aggregation. Note both these relationships fall under the category of association. Sample code showing how to create HAS-A relationship in Java follows: 1 <h2>Choosing between the two</h2> Choosing between is-a and has-a relationship depends upon the actual requirement. When the relationship has to a parent-child relationship than it is suitable to go for is-a relationship. But when the relationship involves a container and an entity then it is best to go for has-a relationship. For example: a car is a vehicle and has a gearbox. Here we should be having an inheritance relationship between car and vehicle class the the relationship between car and a gearbox will be of type composition. Thus the differentiating part is that whether in real world the relationship between the two entities is of type container or parent-child. It is important to analyze the type of relationship before coding the application because if the changes are done at a later stage then it costs more to project. <h2>UML Notation</h2> In UML, the inheritance relationship easily an arrow followed by a blank arrowhead. On the other hand the composition relationship is an arrow followed by a filled diamond. In case of inheritance relationship the arrowhead needs to be pointing at the entity which is being inherited but in case of position relationship the arrowhead needs to be pointing at the contained object. Both these notations are shown below. Inheritance: <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/inheritance.png" alt="inheritance and UML" width="111" height="26" class="size-full wp-image-2054" /> inheritance and UML Composition <img src="http://www.javaexperience.com/wp-content/uploads/2012/12/composition.gif" alt="composition and UML" width="108" height="30" class="alignnone size-full wp-image-2055" />
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.