class Class( _Parameter [= Default], ... ) % Minor( ... )
Through class inheritence, methods can be called with either the type of the inherited class or the type of the class that inherits it. A virtual method allows an inherited method to be overridden in some circumstances and not in others. This mechanism is required to allow inherited classes to be extended, yet allow the underlying methods to coexist with their extended counterparts.
When a method is inhertited from another class it can be overridden by a virtual method. This allows the a call to the method to invoke either the inherited method or the virtual method depending on the context. This is best explained by an example.
class Minor() :First parameter of these methods has the Minor type. generic Do.It :Always invoked with the Minor type. generic To.It :Invoke with either the Major or Minor type. generic Again :Invoke with either the Major or Minor type. end class Major() % Minor :The first parameter of these methods is Major. generic Do.It :Always invoked with the Major type. virtual To.It :Always invoked with the Major type. end
When Do.It is called and type of the the first argument is Minor, the version in the Major class is invoked. If the first argument has the type Minor the version in the Minor class is called. Other methods with a parameter whose type is Minor can receive arguments that are either Major or Minor types since Major inherits Minor.
DO.IT Minor :Invoke the version in the Minor class. DO.IT Major :Invoke the version in the Major class. AGAIN Minor :Not overriden; called with the Minor type. AGAIN Major :Via inheritance; called with the Major type.
The To.It method is declared Virtual in the Major class. When To.It is called and the type of the first argument is Major the version in the Major class is called, as is the case with the non-virtual method, Do.It. However, if the To.It method is called and the first argument is Minor, either version may be called depending on the context. In In the default context, the version in the Minor class is called.
TO.IT Minor :Default context invokes the Minor class version.
The context changes when any method whose first parameter has the type Minor is called and the first argument passed in is Major. The context reverts back to the previous context after the call. Once the context has changed, if the To.It method is invoked with the Minor type, the version in the Major class is invoked instead.
: Initially the default context is established. : This call changes the context of the Minor class to use the Major type. SUBSYTEM Major :First parameter of Subsystem has the Minor type. : After the call the default context is re-established. method SUBSYSTEM entry Minor :The actual argument in this case has the Major type. TO.IT Minor :Invokes the virtual method in the Major class. return
Presumably, the variable passed to the virtual method is actually the same argument passed to the method that caused the context to change. If it was instead actually another variable with a Minor type, there would be problems. To help avoid this, virtual methods are generic and only the first parameter in a method may be passed to a virtual method. Still, virtual methods must be carefully applied. This can only cause problems in cases where an inherited class is extended with additional data structure fields.
: Initially the default context is established. : This call changes the context of the Minor class to use the Major type. SUBSYTEM Major :First parameter of Subsystem has the Minor type. : After the call the default context is re-established. method SUBSYSTEM :This example shows an invalid virtual method call. entry Minor :The actual argument in this case has the Major type. local Other Minor :A local variable declared with the Minor type. SUBROUTINE Other :It is passed to an intermediate method. return method SUBROUTINE :The Minor class context uses the Major type. entry Minor :The actual argument in this case has the Minor type. TO.IT Minor :The virtual version is erroneously called. return