Question
· Oct 30, 2023

Is it possible to call a superclass method outside the method with same name ?

The following code call the method of the same name as defined in the nearest superclass:

Class MyClass Extends %Persistent
{
  ClassMethod Foo()
  {
  }
}

Class SubClass Extends MyClass
{
  ClassMethod Foo()
  {
      do ##super() // <----
  }
}

Not let's say I want to call Foo() super class method but from another method :

Class SubClass Extends MyClass
{
  ClassMethod AnotherMethod()
  {
      do ##super().Foo() //will not work
  }
}

This is possible in some other programming languages such as C# or Java.

I cannot find anything in documentation

Product version: IRIS 2021.1
$ZV: IRIS for Windows (x86-64) 2021.1 (Build 215U) Wed Jun 9 2021 09:39:22 EDT
Discussion (8)3
Log in or sign up to continue

Well, the example you used uses ClassMethod, which is like an any static method in other languages, and can be called directly with no issues. So, this definitely will work

ClassMethod AnotherMethod()
{
  do ##class(MyClass).Foo()
}

If you would want to do the same, but using instance methods, it can be done as well

Assuming the super class, like this

Class dc.MyClass Extends %RegisteredObject
{

Property Value As %String;

Method Foo()
{
  Write !,"MyClass:Foo - ", ..Value
}

}

and child class

Class dc.SubClass Extends MyClass
{

Method Foo()
{
  Write !,"SubClass:Foo - ", ..Value
}

ClassMethod AnotherClassMethod()
{
  set obj = ..%New()
  set obj.Value = "demo"

  Do obj.Foo()
  
  Write !,"-----"
  Do ##class(dc.MyClass)obj.Foo()

  Write !,"-----"
  Do obj.AnotherMethod()
}

Method AnotherMethod()
{
  Do ##class(dc.MyClass)$this.Foo()
}

}

The output will be this

USER>do ##class(dc.SubClass).AnotherClassMethod() 
SubClass:Foo - demo
-----
MyClass:Foo - demo
-----
MyClass:Foo - demo

And as you can see, the last two calls are working from a super class, and it keeps access to the object