EA = new(); has given handle to object of type my_ea to class variable EA.
A=EA; This pass the same handle (pointer value which points to object of my_ea) to A. so , A.member1 should refer to value 2.
Declaring something local does do what you say, but it isnt to do wiht the OPs question.
The OPs is a question of scope.
Declaring the variable as local will just cause an error. It wont return the value of 2.
- - - Updated - - -
The easiest way around this would be to have a virtual function that returns the member1 variable:
Code:
class my_a;
int member1 = 1;
virtual function int get_member1();
return this.member1;
endfunction get_member1;
endclass
class my_ea extends my_a;
int member1 = 2;
virtual function int get_member1();
return this.member1;
endfunction get_member1;
endclass
my_a A;
my_ea EA;
EA =new();
A=EA;
A.get_member1(); //returns 2
I understand that the way you described, I can get value 2 for member1.
But my question is different.
EA = new(); has given handle to object of type my_ea to class variable EA.
A=EA; This pass the same handle (pointer value which points to object of my_ea) to A. so , A.member1 should refer to value 2.
This is because you declared A as class my_a, therefore you only have visibility of variables/functions declared in the my_a class. If you want to see member1 from the my_ea class you will need to cast A to the my_ea class.
This is important because you can have many sub-classes of my_a, with an array of variables all of different classes. The compiler can only see the class of the variable type, it doesnt know what class it is from the handle.
This can get a little confusing with virtual/non virtual functions. non-virtual functions will always call the function from the class type of the variable. virtual functions will always call the function from the class declared by the handle, because they get overridden in the base classes.