Question Norman W. Freeman · 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

Comments

Luis Angel Pérez Ramos · Oct 30, 2023

The method is in your child/extended class as it is in the parent class, so you just need:

do..MethodName(args)
0
Norman W. Freeman  Oct 30, 2023 to Luis Angel Pérez Ramos

This will call the child/extended method and the superclass method, I want to bypass that and call base/super method directly.

0
Dmitry Maslennikov · Oct 30, 2023

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

0
Norman W. Freeman  Oct 30, 2023 to Dmitry Maslennikov

Thanks Dimitry for clarifications.

I did not know about $this syntax. This is indeed explained here.

0
Robert Cemper · Oct 30, 2023

instead of strange constructs that are hard to follow,
I would just take the pragmatical way and add a param SUPER=0

Class SubClass Extends MyClass
{
  ClassMethod Foo(SUPER = 0 )
  {
   if SUPER  do##super() quit
  . . . . . 
      do##super() // <----
  }
}

The efficiency is evident also to less sophisticated programmers
 

0
Robert Cemper  Oct 30, 2023 to Robert Cemper

From my IBM-360-Assembly programming times in the late 60ties

If your code is running hard,
apply a switch to make it smart

 

0
John Genoese  Apr 8, 2024 to Robert Cemper

90ECD00C, my friend.

0