IS-A and HAS-A Relationships

Reading Time: < 1 minute

IS-A Relationship

in OOP this sort of relationship refers to inheritance, both class and interface types. In the hierarchy tree, traversing up to determine whether the relation corresponds to IS-A.

Assume the situation simply; Class A implements Interface B, then we can say Class A IS-A Interface B, this is to traverse up, and the reverse is not true because it is traverse down.

is-a-relation-uml_diagram

public interface Creature{}

public interface Animal extends Creature{}

public interface Movable{}

public class Dog extends Animal implements Movable{}

Correct

  • Dog IS-A Animal,
  • Dog IS-A Movable,
  • Animal IS-A Creature.

Incorrect

  • Creature IS-A Animal (Traverse down doesn’t count),
  • Movable IS-A Creature (There is no relation defined in between),
  • Animal IS-A Dog (Traverse down doesn’t count),
  • Animal IS-A Movable (There is not traverse up relation,  there is only connection via Dog, but since we cannot traverse down, this doesn’t count either)

HAS-A Relationship

This is simply to define an instance of an object in a different object and instantiation doesn’t matter. This type of relation is also known as composition. Assume that we have Car and Engine Objects. A car object having an Engine instance defined.

 

public class Engine {}

public class Car {
	Engine engine;
}