# Method Overriding

**URL:** <https://verificationacademy.com/forums/t/method-overriding/38882>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, Method-Overriding, methods\
**Created:** [March 23, 2021, 10:10am UTC](https://verificationacademy.com/forums/t/method-overriding/38882 "2021-03-23T10:10:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [March 23, 2021, 10:10am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/1 "2021-03-23T10:10:31Z")

</div>

What are the things to be remembered for the method overriding?  
Should I keep the same declaration in my extended class as in my base class?

---

<div class="post-metadata">

**Author:** ![shanthi](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@shanthi](https://verificationacademy.com/forums/u/shanthi)\
**Post date:** [March 23, 2021, 10:51am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/2 "2021-03-23T10:51:53Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/method-overriding/38882):_  
To override the methods the following rules has to be followed

1. The method prototype should be same in both base class & extended class.
2. The method should be declared as virtual in the base class
3. The base class handle should point to extended class object.

```verilog

class base;

virtual function void display();
$display("I AM IN BASE CLASS");
endfunction

endclass

class child extends base;

virtual function void display();
$display("I AM IN CHILD CLASS");
endfunction

endclass

base b_h;
child c_h;

initial begin
c_h=new();
b_h =c_h;
b_h.display;//o/p=I AM IN CHILD CLASS
end

```

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [March 23, 2021, 11:36am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/3 "2021-03-23T11:36:57Z")

</div>

_In reply to [shanthi](https://verificationacademy.com/forums/t/method-overriding/38882/2):_

Hi Shanthi,

The content you explained here is **polymorphism**. Isn’t it?  
Isn’t polymorphism and method overriding different?

---

<div class="post-metadata">

**Author:** ![run2prem](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@run2prem](https://verificationacademy.com/forums/u/run2prem)\
**Post date:** [March 26, 2021, 12:01pm UTC](https://verificationacademy.com/forums/t/method-overriding/38882/4 "2021-03-26T12:01:38Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/method-overriding/38882/3):_

Its one and the same.  
Polymorphism has two aspects : Method overloading and Method overriding.

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [March 26, 2021, 12:25pm UTC](https://verificationacademy.com/forums/t/method-overriding/38882/5 "2021-03-26T12:25:06Z")

</div>

_In reply to [run2prem](https://verificationacademy.com/forums/t/method-overriding/38882/4):_

Can you elaborate your point. With an example if possible

---

<div class="post-metadata">

**Author:** ![harsh\_pandya](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/harsh_pandya/32/2973_2.png) [@harsh\_pandya](https://verificationacademy.com/forums/u/harsh_pandya)\
**Post date:** [March 26, 2021, 6:44pm UTC](https://verificationacademy.com/forums/t/method-overriding/38882/6 "2021-03-26T18:44:59Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/method-overriding/38882/5):_

Overloading:- When two or more methods in the same class have the same name but different parameters, it’s called Overloading.

Overwriteing:- When the method signature (name and parameters) are the same in the superclass and the child class, it’s called Overriding.

I think SV not supports overloading.  
If you try to declare method with same name twice inside same class. you will get compilation “already exists” error.

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [March 27, 2021, 6:24am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/7 "2021-03-27T06:24:02Z")

</div>

_In reply to [harsh pandya](https://verificationacademy.com/forums/t/method-overriding/38882/6):_

Yeah you are right. Method overloading is not present in SV. Its a concept in OOPs initially used in C++ as I know.

For method overriding, we don’t have to declare the method as virtual.

As per my understanding method overriding happens when we declare a method in derived class with same signature. That time the method call will implement the derived class method definition instead of base class one.

I attach a method overriding code here for reference

```verilog
class base;
virtual function void display();
	$display("Base");
endfunction :display
endclass :base

class derived extends base;
function void display();
	$display("Derived");
endfunction :display
endclass :derived

module tb;
	base b1;
	derived d1;
	
	initial begin
		b1=new();
		d1=new();
		//Display of base
		b1.display();
		//Display of derived
		d1.display();		
	end
endmodule :tb

```

---

<div class="post-metadata">

**Author:** ![harsh\_pandya](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/harsh_pandya/32/2973_2.png) [@harsh\_pandya](https://verificationacademy.com/forums/u/harsh_pandya)\
**Post date:** [March 28, 2021, 5:42am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/8 "2021-03-28T05:42:19Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/method-overriding/38882/7):_

Your understanding is correct.  
Just, I want to add something in your example.

Even you have not declare method as virtual,but still it is virtual inside child class.  
Because, if you declare virtual in base class it will reflect it’s all child class.  
So, I suggest not declare method as virtual if you want to use only overwrite and not to use Polymorphism to avoid confusion.

```verilog

base b1,b2;
derived d1;
 
initial begin
  b1=new();
  d1=new();
  b2 = d1;
  //Display of base
  b1.display();
  //Display of derived
  d1.display();
  //Display of derived
  b2.display();
end

```

Thanks,  
Harsh

---

<div class="post-metadata">

**Author:** ![run2prem](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@run2prem](https://verificationacademy.com/forums/u/run2prem)\
**Post date:** [March 28, 2021, 8:25am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/9 "2021-03-28T08:25:31Z")

</div>

_In reply to [harsh pandya](https://verificationacademy.com/forums/t/method-overriding/38882/8):_

No, your understanding here is totally incorrect. SV supports method overloading.

Overloading:- When two or more methods in the same class or derived classe have the same name but different parameters, it’s called Overloading.

When it’s in the same class, it’s called compile time polymorphism. If it’s in derived class it’s runtime polymorphism. Even overriding is a run time polymorphism

SV supports both run time polymorphism while doesn’t support compile time polymorphism.

Virtual is the main keyword to establish overriding

---

<div class="post-metadata">

**Author:** ![harsh\_pandya](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/harsh_pandya/32/2973_2.png) [@harsh\_pandya](https://verificationacademy.com/forums/u/harsh_pandya)\
**Post date:** [March 28, 2021, 8:56am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/10 "2021-03-28T08:56:04Z")

</div>

_In reply to [run2prem](https://verificationacademy.com/forums/t/method-overriding/38882/9):_

Can you please help to explain Overloading with example.

---

<div class="post-metadata">

**Author:** ![run2prem](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@run2prem](https://verificationacademy.com/forums/u/run2prem)\
**Post date:** [March 28, 2021, 9:06am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/11 "2021-03-28T09:06:10Z")

</div>

_In reply to [harsh pandya](https://verificationacademy.com/forums/t/method-overriding/38882/10):_

```verilog

class base;
 
function void display();
$display("I AM IN BASE CLASS");
endfunction
 
endclass
 
class child extends base;
 
function void display(int a); //Note this can have same signature as base as well
$display("I AM IN CHILD CLASS a =%0d",a);
endfunction

function try (); //within the 'child' scope, both methods are visible but resolved at run time based on the context.
  super.display (); 
  display (1);
endfunction
 
endclass
 
base b_h;
child c_h;
 
initial begin
c_h=new();
c_h.try ();
end

```

---

<div class="post-metadata">

**Author:** ![harsh\_pandya](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/harsh_pandya/32/2973_2.png) [@harsh\_pandya](https://verificationacademy.com/forums/u/harsh_pandya)\
**Post date:** [March 28, 2021, 9:37am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/12 "2021-03-28T09:37:00Z")

</div>

_In reply to [run2prem](https://verificationacademy.com/forums/t/method-overriding/38882/11):_

I understand your point with super. This is one of way to access parent method from child class.  
But, for this you need to add some extra logic ( like try method ).  
So, we can not say it is directly overloading as like c++.  
However, In my previous reply my meaning was like, we can not overload method like c++ in same class in SV.  
Like below example.  
[C++ Overloading - javatpoint](https://www.javatpoint.com/cpp-overloading).

Thanks,  
Harsh

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [March 30, 2021, 4:34am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/13 "2021-03-30T04:34:24Z")

</div>

_In reply to [run2prem](https://verificationacademy.com/forums/t/method-overriding/38882/12):_

Method overloading is the practice of declaring the same method with different signatures. The same method name will be used with different data type . This is Not Supported by SystemVerilog as of 2008

---

<div class="post-metadata">

**Author:** ![run2prem](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/r/3ec8ea/32.png) [@run2prem](https://verificationacademy.com/forums/u/run2prem)\
**Post date:** [March 30, 2021, 6:52am UTC](https://verificationacademy.com/forums/t/method-overriding/38882/14 "2021-03-30T06:52:22Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/method-overriding/38882/13):_

Within the derived class context, it is visible to two methods of same name but with different signature. This is still a kind of overloading but I believe/understand this to be yet another run-time polymorphism. In actual testbenches, we dont use this concept much unless a special requirement demands it.

Also, we dont need another method ‘try’ which is just an example. Even within the same display method or anywhere within the class scope both display methods are visible. Yes, its scope limited.

What you are looking is compile-time polymorphism (where the simulator/compiler purely resolves at compile time based on method signature). As you stated, SV dont support this

My point is

1. Without virtual in base class, it will exhibit the above kind of overloading
2. With virtual in base class, it will exhibit overriding.

Regards,  
Prem
