The knowledge sharing zone -

Difference between IS-A and HAS-A relationships

This page contains the details about What is the difference between IS-A and HAS-A relationships, Their comparison and various real time examples of IS-A and HAS-A relationships.

In Object Oriented Programming, There are two main categories of relationships:

  1. IS-A relationship
  2. HAS-A relationship

Following is the difference and comparison between IS-A and HAS-A relationship:

IS-A Relationship HAS-A Relationship
In OO design, Inheritance is represented by an is-a relationship. In OO design, the has-a relationship is represented by composition, association or aggregation.
  • Consider a Shape example where Circle, Square and Star all inherit directly from Shape.
  • This relationship is often referred to as an is-a relationship because a circle is a shape, and Square is a shape.
  • When a subclass inherits from a superclass, it can do anything that the superclass can do.Thus, Circle, Square and Star are all extensions of Shape.
  • Consider a Car example, Car has an engine. Both the engine and the car are separate, potentially standalone objects.
  • However,the car is a complex object that contains (has an) engine object.
Increases the coupling between different objects - Tightly coupled code. Promotes low coupling between different objects - Loosely coupled code.
Difficult to maintain the  the code extensibility, modifiablity and maintainability. Increases the code extensibility, modifiablity and maintainability.
Code becomes more error prone in case of multi level class hierarchies. Indirectly help to reduce error prone code.
  • Inherits the states and functions of parent class.
  • Indirectly Child class is also indirect owner of parent members.
  • Both the classes are dependent on each other, means to say Life or existance of classes are not indepedent.
  • One class is using another class by its instance or object or
  • Asks other class to serve its functionality, And both the classes are independent.
  • Life or existance of classes are indepedent.
More details about IS-A relationship - Inheritance found here. Find here more detail about CompositionAssociation and Aggregation (HAS-A relationship).


You would also like to read:

What is Realization?
What is Generalization?
What is Inheritance?
What is Association?
Aggregation Vs Composition Vs Association
What is Composition?
What is Aggregation?

Comments :

Thanks a lot, nice tutorial...

up vote icon down vote icon

Nice tutorial.,. thanks... :D

up vote icon down vote icon

good ..

up vote icon down vote icon

Nice Tutorial

up vote icon down vote icon
Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow