Difference between inheritance and polymorphism

Hi,

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

Thanks
Debasmita

In reply to Debasmita:

Two resources you should review:

Using Parameterized Classes and Factories: The Yin and Yang of Object-Oriented Verification

SystemVerilog OOP for UVM Verification(especially the second session)

In reply to Debasmita:

Consider this code snippet to understand,

Inheritance -

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 -

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.

In reply to mayurkubavat:

In reply to Debasmita:
Consider this code snippet to understand,
Inheritance -

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 -

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

*In reply to Debasmita:*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.

1 Like