In the context of inheritance, if property from base class is inherited, is it legal to access the property using super?
I was looking at a code that derives from a parent class and then sets some value using super.
As this was really confusing, I took a closer look and realized that it was not necessary to use super.
Please see one simple example below:
module x;
class c1;
int x = 10;
endclass
class c2 extends c1;
function disp;
$display ("x %d x %d", x, super.x);
endfunction
endclass
initial begin
c2 c2_obj;
c2_obj = new();
c2_obj.disp();
end
endmodule
I assume, accessing x as a local property or as super.x are both legal but any reason why one would access it using super.x?
“super.x” means look for the x property from the context of the base class(c1)that this class (c2) is extended from. We do this all the time with class methods because the method name exists in both the extended class and the base class. We would need to do this if the property x existed in both base and extended classes and needed to distinguish between the two. This is very rare.
In reply to verif_learner:
“super.x” means look for the x property from the context of the base class(c1)that this class (c2) is extended from. We do this all the time with class methods because the method name exists in both the extended class and the base class. We would need to do this if the property x existed in both base and extended classes and needed to distinguish between the two. This is very rare.
Dave,
Sorry, my question wasn’t probably clear. I understand the use of super.
Let me explain.
class base has a property x
class derived extends base but does not additionally define x additionally
Now from derived class, I can directly refer to x property without using super.
What is the point of using super.x from derived class?
Does it make any difference? Is it for readability?
PS: I ask this because I see usage of this in one of the code I am re-using.