Hi @dave_59
While going through SystemVerilog LRM, Chapter 8.14 Overridden Members, it is mentioned that when Base class members (i.e. properties and methods) are overridden (i.e. defined again with the same name) in an extended class and when assignment Base = Extended is done, then references to Base access the properties and methods of the Base class instead Extended class (Example 1: Overridden members).
And members are not overridden, then references to Base access the properties and methods of the Extended class (Example 2: Non-overridden and overridden members).
So, I have created two examples below and need clarification on the 2nd example.
Example 1: Overridden Members
In this example, I have overridden both property and methods in an extended class. Then trying to access property and methods using Base handle after Base = Extended.
module top;
class Base;
integer i = 1;
function integer get();
get = i;
endfunction
function void display();
$display("Base: I: %0d", i);
endfunction
endclass
class Extended extends Base;
integer i = 2;
function integer get();
get = -i;
endfunction
function void display();
$display("Extended: I: %0d", i);
endfunction
endclass
initial begin
Base b; Extended e;
b = new(); e = new();
$write("Before assigning Extended to Base\n");
b.display(); // Call Base::display()
e.display(); // Call Extended::display()
$write("After assigning Extended to Base\n");
b = e;
b.display(); // Call Base::display() - because of non-virtual method
e.display(); // Call Extended::display()
j = b.i; // Access Base::i - As per LRM 8.14
$display("Value of J: %0d", j);
j = b.get();
$display("Value of J: %0d", j);
end
endmodule
Output: Example 1: Overridden members
Output is as expected
# Before assigning Extended to Base
# Base: I: 1
# Extended: I: 2
# After assigning Extended to Base
# Base: I: 1
# Extended: I: 2
# Value of J: 1
# Value of J: 1
Example 2: Non-overriden and overriden members
In this example, only method has overridden in an extended class.
module top;
class Base;
integer i = 1;
function void display();
$display("Base: I: %0d", i);
endfunction
endclass
class Extended extends Base;
function void display();
$display("Extended: I: %0d", i);
endfunction
endclass
initial begin
integer j;
Base b;
Extended e;
b = new();
e = new();
$write("Before assigning Extended to Base\n");
b.display(); // Access Base::display() - I = 1 (as expected)
e.display(); // Access Extended::display() - I = 1 (as expected)
b = e;
e.i = 2;
$write("After assigning Extended to Base\n");
b.display(); // Access Base::display() - So, should print I = 1, but I = 2 is printed (not as expected)
e.display(); // Access Extended::display() - I = 2 (as expected)
j = b.i; // Access Extended::i (as expected)
$display("Value of J: b.i: %0d",j); // J = 2 (as expected)
end
endmodule
Output: Example 2: Non-overridden and overridden members
Output is not expected when b.display() is called after b = e;
# Before assigning Extended to Base
# Base: I: 1
# Extended: I: 1
# After assigning Extended to Base
# Base: I: 2
# Extended: I: 2
# Value of J: b.i: 2
So, my query is why the value of I is printed as 2 when called b.display(); after b = e; statement ? I am expecting the value of I as 1 because of the following reasons:
First: The display() method is overridden in an extended class, so references to b access the methods result in accessing Base class method and should print 1.
Second: The display() method is non-virtual, so Base class method should be called and shoud print 1.
Thanks,
Naveen Kadian