Method Overriding

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?

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

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


In reply to shanthi:

Hi Shanthi,

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

In reply to bachan21:

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

In reply to run2prem:

Can you elaborate your point. With an example if possible

In reply to bachan21:

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.

In reply to harsh pandya:

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

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

In reply to bachan21:

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.


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

In reply to harsh pandya:

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

In reply to run2prem:

Can you please help to explain Overloading with example.

In reply to harsh pandya:


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

In reply to run2prem:

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.

Thanks,
Harsh

In reply to run2prem:

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

In reply to bachan21:

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