Overriding Properties

A property of a superclass can be overridden by a subclass by providing a different implementation.

The properties of a class are implemented by the keywords Get and Set, or by access to an otherwise private instance variable.

To access the property, you do not have to specify Get or Set: the context tells the processor which task you're doing. To create read-only properties, specify only a Get method, with no Set method, or for instance variable properties, specify Readonly.

For an instance variable property, there are two different ways for method code in the class itself to refer to the property:

  • %This.PropertyName

  • &PropertyName

The first way is dynamically bound, as it uses whatever (possibly overridden by a subclass) implementation of PropertyName that the current object provides.

The second way always accesses the private instance variable of the class that the method is declared in.

The following example returns strings, by converting the property. It also demonstrates overriding.

class A
   property number Anum;
   property string Astring get;
   property string Astring2 get;
end-class;

/* The following is a dynamic reference to the Anum property of the current⇒
 object, which might be a subclass of A, so this might not access the A⇒
 class's implementation of Anum as an instance variable. */
Get Astring
   return String(%This.Anum);
end-get;

/* The following is a static reference to our &Anum instance variable. Since it⇒
 does not use %This, it will not be overridden even if the current object is a⇒
 subclass of A. */
Get Astring2
   return String(&Anum);
end-get;

/* The following overrides the Anum property of our superclass, 
in this case by providing another instance variable implementation. 
This gives us our own &Anum instance variable. */
class B extends A
   property number Anum;
end-class;
. . .
/* Set the Anum property in B. Because of the above definitions, this sets 
the &Anum instance variable in the B object, but not the &Anum instance 
variable in its superobject. In the absence of other setting, the latter will 
be defaulted to 0. */ 

local B &B = Create B();
&B.Anum = 2;

&MyValue = &B.Astring; /* &MyValue is "2" */
&MyValue = &B.Astring2; /* &MyValue is now "0" */