Hi,
How to Call Only Parent Method with child Handle.
Below is the basic snippet. I want to call parent display method only, using child handle.
super keyword in parent handle will call both. -:
class base_class;
function void display();
$display("Inside base");
endfunction
endclass
class ext_class_1 extends base_class;
function void display();
$display("Inside extended class 1");
endfunction
endclass
You are confusing the terms ‘base’/‘extended’ with ‘parent’/‘child’. There is a distinct difference between these terms in SystemVerilog. An ‘extended’ class inherits all of the properties of a ‘base’ class. A ‘parent’ class contains an instance of a completely different ‘child’ class.
Your question should be phrased as “How to call a base-class method with an extended-class handle”.
To accomplish this, you assign the extended class handle to a base class handle, and call the required function:
class base_class;
function void display();
$display("Inside base");
endfunction
endclass
class ext_class_1 extends base_class;
function void display();
$display("Inside extended class 1");
endfunction
endclass
module testbench();
ext_class_1 my_ext_class;
base_class my_base_class;
initial begin
my_ext_class = new();
my_ext_class.display();
my_base_class = my_ext_class; // Assign the extended class handle to the base class handle
my_base_class.display();
end
endmodule
Hi,
Thanks for the terminology clarification, But my question was -:
"With extended class handle(my_ext_class) can we somehow call base class display method
Like -:
my_ext_class.display – should print “Inside base”
initial begin
my_ext_class = new(); line1
my_ext_class.display(); line2
my_base_class = my_ext_class; line3 // Assign the extended class handle to the base class handle line4
my_base_class.display(); line5
end
Line2 will display “inside extended class”
Line5 - either you write line3 or not, my_base_class will always display “inside base”.
I want to call base method(ie inside base) using my_ext_class handle , how it is possible is my question ?
The reason I think to implement this is -:
In the source code(already written by other person), has defined function of same name with different behavior in base and extended class.
he is using extended class handle to call that function everywhere in source code.
So I was just asking is it possible to call base class function with extended class handle.
Currently workaround which I did is -:
I Added super.function_name in extended class method and then given return 0
Something like this -:
class base_class;
function void display();
// source code
$display(“Inside base”);
endfunction
endclass
class ext_class_1 extends base_class;
function int display();
super.display();
return 0;
// Source Code
$display(“Inside extended class 1”);
endfunction
endclass
Hi i guess correct approch is to replace
Extended class handle with base class everywhere in source code.
Then in future if wanted to call extended method,that can be done through
Be implemented through Polymorphism.