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
Comments
The method is in your child/extended class as it is in the parent class, so you just need:
do..MethodName(args)This will call the child/extended method and the superclass method, I want to bypass that and call base/super method directly.
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
Thanks Dimitry for clarifications.
I did not know about $this syntax. This is indeed explained here.
And here also a bit, about this feature
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
From my IBM-360-Assembly programming times in the late 60ties
If your code is running hard,
apply a switch to make it smart
90ECD00C, my friend.