2015 Apr 15 2:38 PM
Hi all,
I know static methods cannot be redefined, but they can be called using a subclass as the 'calling class name'. Is it possible to determine the object type used for calling a static method?
For example, I have ZCL_SUPER with a static method HELLO and a subclass ZCL_SUB.
I can use ZCL_SUPER=>HELLO( ) or I can use ZCL_SUB=>HELLO( ).
Both will execute the same code (correct), but is it possible to tell the difference inside the HELLO method? SY-REPID will always show the superclass (also correct).
I've found some similar threads but none with a clear answer.
Any input appreciated,
Mike
2015 Oct 23 7:41 AM
I guess there is no simple solution. I expect that the compiler interprets the symbolic names ZCL_SUPER=> and ZCL_SUB=> in order to point to the method and forgets about it. Why should it keep the information? In fact I have a similar problem: Is there a simple way to find out the name of an actual parameter passed to a formal parameter inside a method? It isn't! I ended up in interpreting the call stack and parsing the calling code!
Hi Horst,
Great, so two parts of the SAP Help give mixed messages
Of course as SAP created ABAP, they are at liberty to implement their own views of OO. If redefinition of static methods were allowed - as it is in Java - then all this would not be an issue. I accept that SAP have made the decision not to allow it.
Static methods are not completely detached from their class, they form part of the scope of the other class components and can access e.g. a protected static attribute of the class. So even though there is no object, static components form part of the class 'bubble' (I'm writing a diagram here ).
So although technically the runtime redirects the call to the superclass, semantically it is wrong, because the intent of someone who writes ZCL_SUB=> is to touch class ZCL_SUB, otherwise s/he could just write ZCL_SUPER.
Regarding a static constructor, there are no objects and its task is to set the static attributes before they are needed the first time. And that is fullfilled in ABAP. Prove me wrong.
This is precisely what isn't happening and what creates the confusion. The code specifically refers to class ZCL_SUB, because I want ZCL_SUB's class constructor to execute. It sets different attributes to ZCL_SUPER. But I still end up with the attributes of ZCL_SUPER. This is the bit that is counterintuitive.
Regards,
Mike
2016 Mar 23 8:15 AM
Thanks Horst,
I was looking on CL_DEMO_OUTPUT before, but I thought that maybe there is other way Now I need some time to adapt this to my need.
Cheers
Łukasz
2016 Mar 23 8:35 AM
Moderator message: in future, please don't add your question to a question that's more than a few months old. Rather create a new question and link to the old.
| User | Count |
|---|---|
| 4 | |
| 2 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |