Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling super method dynamically

michael_halfmann
Explorer
0 Kudos

Hi,

I would like to call a method on the super class dynamically in order to be able to provide the parameters in a table. So I tried something like:

CALL METHOD super->( 'MY_METHOD' )

PARAMETER-TABLE lth_parameter.

This leads to the error message "Mit SUPER-> kann nur die vorige Implementierung der eigenen Methode aufgerufen werden.". MY_METHOD is the same method as the one in which I try to place this call but it still complains. However, I can not call it statically since I have quite some optional parameters to provide and there are many different combinations of IS SUPPLIED logics in the called super method.

Does anyone know hot to call the super method dynamically or statically plus providing the parameters as a table?

Kind regards,

Michael

8 REPLIES 8

Former Member
0 Kudos

Hi Michael,

i haven't figured out yet, which part you want to do dynamically.

Do you want to decide whether it's got to be

me->method or

super->method

Or is it mainly the call with parameter table?

Your error implies that the method, you try to call has not been redefined in your actual method. Check whether you get the same error message when you call the method statically.

Regards

JW

0 Kudos

Hi Jörg,

my aim is to be able to call the method with a parameter table, therefore I have to call it dynamically. Or is there a way to call it statically but still providing the parameters via a table?

It has to be super->.... The method is actually redefined so I can call it statically using the statement SUPER->MY_METHOD( ... ). This leads to no syntax error.

Kind regards,

Michael

matt
Active Contributor
0 Kudos

I think you're going to have to break the inheritance, and code the "super" functionality in a differently named method of the superclass, with the same interface.

matt

0 Kudos

Hi Michael,

parameter tables are restricted to dynamic calls, but still

you could try to make a semi dynamic call like this:

DATA: mymethod type string value 'DO_SOMETHING'.

call method SUPER->(mymethod) PARAMETER-TABLE ptab

Define ptab of type ABAP_PARMBIND_TAB (remember to include type-pool ABAP to your class definition).

Give one row for each parameter composed of:

NAME name of formal parameter in capitals

KIND one of (I)MPORTING; (E)XPORTING; (C)HANGING; (R)ETURNING/(R)ECEIVING

VALUE ref to data that points to actual paramter

that should do the trick

Regards

JW

matt
Active Contributor
0 Kudos

I tried that, but couldn't get it to work. Have you actually got it working?

matt

0 Kudos

Hello Michael

It tried this too and it did not work. Since we can call only the very same method in the superclass is does not make sense to allow dynamic calling of this SUPER method. At least this is my explanation of the observed behaviour.

I am not really sure if the following sample report ZUS_SDN_CALL_METHOD_DYNAMIC is helpful at all but it might give you some new ideas.

&----


*& Report ZUS_SDN_CALL_METHOD_DYNAMIC

*&

&----


*&

*&

&----


REPORT zus_sdn_call_method_dynamic.

TYPE-POOLS: abap.

----


  • CLASS lcl_superclass DEFINITION

----


*

----


CLASS lcl_superclass DEFINITION.

PROTECTED SECTION.

METHODS:

my_method_1

IMPORTING

value(id_text) TYPE clike.

ENDCLASS. "lcl_superclass DEFINITION

----


  • CLASS lcl_subclass DEFINITION

----


*

----


CLASS lcl_subclass DEFINITION

INHERITING FROM lcl_superclass.

PUBLIC SECTION.

METHODS:

constructor

IMPORTING

value(io_parent) TYPE REF TO lcl_superclass.

PROTECTED SECTION.

DATA:

mo_super TYPE REF TO lcl_superclass.

METHODS:

my_method_1 REDEFINITION.

ENDCLASS. "lcl_subclass DEFINITIO

----


  • CLASS lcl_superclass IMPLEMENTATION

----


*

----


CLASS lcl_superclass IMPLEMENTATION.

METHOD my_method_1.

WRITE: / 'SUPER: my_method_1 &', id_text.

ENDMETHOD. "my_method_1

ENDCLASS. "lcl_superclass IMPLEMENTATION

----


  • CLASS lcl_subclass IMPLEMENTATION

----


*

----


CLASS lcl_subclass IMPLEMENTATION.

METHOD constructor.

super->constructor( ).

me->mo_super = io_parent.

me->my_method_1( 'normal call' ).

ENDMETHOD. "constructor

METHOD my_method_1.

DATA:

ld_data TYPE REF TO data,

ld_string TYPE string,

ld_method TYPE string,

ls_param TYPE abap_parmbind,

lt_params TYPE abap_parmbind_tab.

WRITE: / 'SUBCLASS: my_method_1 &', id_text.

super->my_method_1( 'super called' ). " ok

ld_method = 'MY_METHOD_1'.

CALL METHOD me->mo_super->(ld_method)

EXPORTING

id_text = 'super (partially) dynamically called'.

CLEAR: ls_param.

ls_param-name = 'ID_TEXT'.

ls_param-kind = 'E'. " exporting

ld_string = 'super (fully) dynamically called'.

GET REFERENCE OF ld_string INTO ls_param-value.

INSERT ls_param INTO TABLE lt_params.

CALL METHOD me->mo_super->(ld_method)

PARAMETER-TABLE

lt_params.

ENDMETHOD. "my_method_1

ENDCLASS. "lcl_subclass IMPLEMENTATION

DATA:

go_super TYPE REF TO lcl_superclass,

go_sub TYPE REF TO lcl_subclass.

START-OF-SELECTION.

BREAK-POINT.

CREATE OBJECT go_super.

CREATE OBJECT go_sub

EXPORTING

io_parent = go_super.

END-OF-SELECTION.

Regards,

Uwe

0 Kudos

Hi all,

thanks for your answer. I fear that what I want to do is not possible.

The example program calls the method on another object of the superclass. That would be a workaround for some cases. Unfortunately in my case I really need to call the super-method on the same object.

Kind regards,

Michael

0 Kudos

Hello Michael

Indeed, this is one of the very few occasions where I agree to say that something is not possible within SAP.

Regards,

Uwe