# Difference between inheritance and polymorphism

**URL:** <https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148>\
**Category:** SystemVerilog\
**Tags:** Polymorhphism, SystemVerilog, Inheritance\
**Created:** [August 11, 2016, 11:32pm UTC](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148 "2016-08-11T23:32:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Debasmita](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@Debasmita](https://verificationacademy.com/forums/u/Debasmita)\
**Post date:** [August 11, 2016, 11:32pm UTC](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148/1 "2016-08-11T23:32:20Z")

</div>

Hi,

Can anybody help me in understanding the main difference between Inheritance and polymorphism ?

Thanks  
Debasmita

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 12, 2016, 12:36am UTC](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148/2 "2016-08-12T00:36:01Z")

</div>

_In reply to [Debasmita](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148):_

Two resources you should review:

[Using Parameterized Classes and Factories: The Yin and Yang of Object-Oriented Verification  
 ](https://resources.sw.siemens.com/en-US/white-paper-using-parameterized-classes-and-factories-the-yin-and-yang-of-object)

[SystemVerilog OOP for UVM Verification](https://verificationacademy.com/topics/systemverilog/systemverilog-oop-for-uvm-verification/)(especially the second session)

---

<div class="post-metadata">

**Author:** ![mayurkubavat](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@mayurkubavat](https://verificationacademy.com/forums/u/mayurkubavat)\
**Post date:** [August 12, 2016, 4:08am UTC](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148/3 "2016-08-12T04:08:58Z")

</div>

_In reply to [Debasmita](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148/2):_

Consider this code snippet to understand,

Inheritance -

```verilog
Class A;
  //Properties and Methods
endclass

Class B extends A;
  //Properties and Methods
endclass

```

By means of inheritance you can reuse previously written classes to extend functionality of your blocks. Best example is UVM base class library. By extending our class from uvm classes we can make use of inherited properties and methods(tlm ports, phases etc.)

Polymorphism -

```verilog
Class A;
  //Properties and Methods
  virtual function void display();
    $display();
  endfunction
endclass

Class B extends A;
  //Properties and Methods
  function void display();
    $display();
  endfunction
endclass

A a_h;
B b_h;

initial
begin
  b_h = new();
  a_h = b_h;
  a_h.display(); //Calls extended class B method display by means of Polymorphism
end

```

- When inheritance is in picture, one can hide parent class properties/methods by declaring same set in extended class as well.
- So, when creating extended class objects and using it in existing testbench (with same old parent class handles) to access these new extended class methods polymorphism is required.

---

<div class="post-metadata">

**Author:** ![Debasmita](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@Debasmita](https://verificationacademy.com/forums/u/Debasmita)\
**Post date:** [August 12, 2016, 12:06pm UTC](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148/4 "2016-08-12T12:06:40Z")

</div>

_In reply to [mayurkubavat](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148/3):_

> _In reply to [Debasmita](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148/3):_  
> Consider this code snippet to understand,  
> Inheritance -

```verilog
Class A;
//Properties and Methods
endclass
Class B extends A;
//Properties and Methods
endclass

```

By means of inheritance you can reuse previously written classes to extend functionality of your blocks. Best example is UVM base class library. By extending our class from uvm classes we can make use of inherited properties and methods(tlm ports, phases etc.)  
Polymorphism -

```verilog
Class A;
//Properties and Methods
virtual function void display();
$display();
endfunction
endclass
Class B extends A;
//Properties and Methods
function void display();
$display();
endfunction
endclass
A a_h;
B b_h;
initial
begin
b_h = new();
a_h = b_h;
a_h.display(); //Calls extended class B method display by means of Polymorphism
end

```

- When inheritance is in picture, one can hide parent class properties/methods by declaring same set in extended class as well.
- So, when creating extended class objects and using it in existing testbench (with same old parent class handles) to access these new extended class methods polymorphism is required.

Hi mayurkubavat,

Thanks for your clarification. But I still have doubts. If we will not declare the fuction as “virtual” then also we can override the function using Inheritance concept. So What is the use of “virtual” keyword here ?

Thanks  
Debasmita

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 12, 2016, 4:24pm UTC](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148/5 "2016-08-12T16:24:26Z")

</div>

\*In reply to [Debasmita](https://verificationacademy.com/forums/t/difference-between-inheritance-and-polymorphism/32148/4):\*Polymorphsm comes into play when you use a base class variable that references an extended object. In mayurkubavat’s example a\_h is a class A variable that has a handle to a type B object. If the function display was not declared as ‘virtual’, then calling a\_h.display would always call the defined in class A, regardless of whether or not a\_h was storing a handle to an extended object.

You really need to check the resources I gave you. Trying to figure this out one question at a time will become very frustrating.
