When you extend any class, the extended class inherits the parents members and methods. Because of which, when you create(new) an extended class, all the members/functions in Parent are also created along with the additional members/functions in the extended class.
So, in your example when child is created (new()), memory will be allocated to two color variables one for parent ‘color’ and one for child ‘color’. And when you try to access color from the respective handles the respective values are printed.
In reply to S.P.Rajkumar.V:
I recommend against using parent and child when referring to OOP inheritance. Parents create(construct) children and they are distinct objects from their parents. When you inherit property, that property becomes yours and all your property belongs to one object. Use base and extended.
The UVM uses terms parent and child to refer to relationships between objects when build a hierarchical tree/graph structure. The class uvm_component has a handle to its parent and handles to all its children so that you can traverse the hierarchical structure. This terminology is used in most programming languages and is independent of OOP.
Thanks for your response.
I am seeing yet another result when constraint block name in child class is renamed as 'c1".
Following is the result:
c1.color = 65 p1.color = 1370007103 c2.color = 65
I am unable to get how p1.color is getting a random value?
Because the original constraint is over-ridden and only one constraint (the one in class child) is honored when this.randomize() is called from calc_crc of class child.
Where-as in earlier case, both constraints were in place applicable for the respective color’s.
It is an extremely bad practice reusing the same variable identifier name in an extended class. You might as well give it a different name because that is how the compiler sees it anyways. It is not and override—it is hiding the original variable name from the extended class. The base variable still exists and all references to that variable exist as well prior to the extended class.