Skip to main content

##super Syntax

##super Syntax

Suppose that a subclass method overrides a superclass method. From within the subclass method, you can use the ##super() syntax to invoke the overridden superclass method.

Note:

##super is not case-sensitive.

For example, suppose that the class MyClass.Down extends MyClass.Up and overrides the Simple class method. If the code for MyClass.Up.Simple() is:

ClassMethod Simple()
{
    Write "Superclass.",!
}

and the code for MyClass.Down.Simple() is:

ClassMethod Simple()
{
    Write "Subclass.",!
    Do ##super()
}

then the output for subclass method, MyClass.Down.Simple(), is:

>Do ##Class(MyClass.Down).Simple()
Subclass.
Superclass.
>

A more generic way to refer to other methods are the $METHOD and $CLASSMETHOD functions, which are for instance and class methods, respectively; these are described in Dynamically Accessing Objects. These provide a mechanism for referring to packages, classes, and methods programmatically.

Calls That ##super Affects

##super only affects the current method call. If that method makes any other calls, those calls are relative to the current object or class, not the superclass. For example, suppose that MyClass.Up has MyName() and CallMyName() methods:

Class MyClass.Up Extends %Persistent
{

ClassMethod CallMyName()
{
    Do ..MyName()
}

ClassMethod MyName()
{
    Write "Called from MyClass.Up",!
}

}

and that MyClass.Down overrides those methods as follows:

Class MyClass.Down Extends MyClass.Up
{

ClassMethod CallMyName()
{
    Do ##super()
}

ClassMethod MyName()
{
    Write "Called from MyClass.Down",!
}

}

then invoking the CallMyName() methods have the following results:

USER>d ##class(MyClass.Up).CallMyName()
Called from MyClass.Up
 
USER>d ##class(MyClass.Down).CallMyName()
Called from MyClass.Down

MyClass.Down.CallMyName() has different output from MyClass.Up.CallMyName() because its CallMyName() method includes ##super and so calls the MyClass.Up.CallMyName() method, which then calls the uncast MyClass.Down.MyName() method.

##super and Method Arguments

##super also works with methods that accept arguments. If the subclass method does not specify a default value for an argument, make sure that the method passes the argument by reference to the superclass.

For example, suppose the code for the method in the superclass (MyClass.Up.SelfAdd()) is:

ClassMethod SelfAdd(Arg As %Integer)
{
    Write Arg,!
    Write Arg + Arg
}

then its output is:

>Do ##Class(MyClass.Up).SelfAdd(2)
2
4
>

The method in the subclass (MyClass.Down.SelfAdd()) uses ##super and passes the argument by reference:

ClassMethod SelfAdd(Arg1 As %Integer)
{
    Do ##super(.Arg1)
    Write !
    Write Arg1 + Arg1 + Arg1
}

then its output is:

>Do ##Class(MyClass.Down).SelfAdd(2)
2
4
6
>

In MyClass.Down.SelfAdd(), notice the period before the argument name. If we omitted this and we invoked the method without providing an argument, we would receive an <UNDEFINED> error.

FeedbackOpens in a new tab