2020 Sep 14 2:58 PM
hello
I have enhanced class with overwrite implementation . I created a core_object->new_method() that is called inside the overwrite so I have access to all attributes.
I need to introduce some new logic and also keep most of the logic from original method. But original method has a calle to super-> which doesn't seem to work in inside my new core_object->new_method()
how can I call super-> from overwrite core_object->new_method()?
is this possible?
2020 Sep 14 4:09 PM
I understand that you implemented an overwrite method (enhancement framework) to change a line of code of the standard.
1) It's not best practice because you won't benefit of automated upgrade checks (SPAU) during the next upgrade.
2) You can't use Super in a class which is not the subclass of this class (an overwrite method belongs to a completely separate local class).
Workarounds:
1) Either do a modification of the standard to benefit of SPAU checks.
2) A bad solution which works but you don't benefit of SPAU checks, is to create an implicit enhancement at the beginning of the standard method, and do a RETURN to not execute the standard code.
2020 Sep 14 3:00 PM
it is not totally clear.
If you have created a new method, in a standard one. Call this new method from a standard method. Why do you need to call super ?
2020 Sep 14 3:20 PM
Hello juanlee337
It's not possible. The SUPER keyword is used to call the same method, but from the parent class.
https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/abapcall_method_meth_super.htm?
Why not create a second child inheriting from the parent, have it overwrite the method and then create the instance of a required type based on some condition? This would be more of an Object Oriented programming way.
So instead of:
CLASS lcl_parent DEFINITION.
PUBLIC SECTION.
METHODS:
method1.
ENDCLASS.
CLASS lcl_child DEFINITION INHERITING FROM lcl_parent.
PUBLIC SECTION.
METHODS:
method1 REDEFINITION,
method2.
ENDCLASS.
DATA(lo_child) = new lcl_child( ).
IF something.
lo_child->method1( ).
ELSE.
lo_child->method2( ).
ENDIF.
You'd have:
CLASS lcl_parent DEFINITION.
PUBLIC SECTION.
METHODS:
method1.
ENDCLASS.
CLASS lcl_child1 DEFINITION INHERITING FROM lcl_parent.
PUBLIC SECTION.
METHODS:
method1 REDEFINITION.
ENDCLASS.
CLASS lcl_child2 DEFINITION INHERITING FROM lcl_parent.
PUBLIC SECTION.
METHODS:
method1 REDEFINITION.
ENDCLASS.
DATA:
lo_logic TYPE REF TO lcl_parent.
IF something.
lo_logic = NEW lcl_child1( ).
ELSE.
lo_logic = NEW lcl_child2( ).
ENDIF.
lo_logic->method1( ).This, BTW, is called a Strategy pattern in Object Oriented design patterns.
Kind regards,
Mateusz
2020 Sep 14 4:47 PM
i dont think this will work in class enahancement framework.
2020 Sep 15 7:50 AM
2020 Sep 14 4:09 PM
I understand that you implemented an overwrite method (enhancement framework) to change a line of code of the standard.
1) It's not best practice because you won't benefit of automated upgrade checks (SPAU) during the next upgrade.
2) You can't use Super in a class which is not the subclass of this class (an overwrite method belongs to a completely separate local class).
Workarounds:
1) Either do a modification of the standard to benefit of SPAU checks.
2) A bad solution which works but you don't benefit of SPAU checks, is to create an implicit enhancement at the beginning of the standard method, and do a RETURN to not execute the standard code.
2020 Sep 14 4:39 PM
Hi Sandra. thanks for you input.
is still not possible if I inherit from original parent class in my new class-enhancement class?
i think your work around #1 might be the best solution because is only 1 line of code that needs to be removed but our company policy as strict guidelines about changing standard code.
For number 2 work around, even if I RETURN , then i still need IOVERWRITE method then because we need this logic. ( the problem is that we sap recently and stardard code behavior is different the previous version).
2020 Sep 14 4:50 PM
Do you mean changing the generated class corresponding to the overwrite method so that to inherit a standard class? Probably it's not possible to do it easily (would be very ugly).
Workarounds
1) tell your company what you are currently doing to get around their policy, and they will understand that it's best to have a modification of the standard. Or just tell your client that you can't do what he wants because of their policy, and let them choose... (but it's a technical debt so the company responsible of the maintenance will pay or will make the company pay)
2) No, I meant to copy the whole code of the method except the line to remove in an implicit enhancement at the beginning of the standard method (it's not a class enhancement/not an overwrite/pre/post method, it's a "hook"), and add a RETURN at the end of this hook so that to leave the standard method and not execute the standard code of this method.
2020 Sep 14 4:59 PM
i see you point to number 2. Never thought of this but yes , not ideal, but this would work.