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: 

Method parameters

Former Member
0 Kudos

Hi all,

Let's say existing there is a class with 3 importing parameters into the method. But somehow there is a needs to extend the existing method into 4 importing parameters.

The question is, if we are applying OOP concept here, how should I do it? Should I create a new class for the 4 importing parameters?

Thanks for the advice in advance.

1 ACCEPTED SOLUTION

uwe_schieferstein
Active Contributor
0 Kudos

Hello Myah

Options (2) and (3) are demonstrated in sample report ZUS_SDN_ABAP_OO_ENH_SAMPLE.


*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ABAP_OO_ENH_SAMPLE
*&
*&---------------------------------------------------------------------*
*& Thread: Method parameters
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1063590"></a>
*&---------------------------------------------------------------------*

REPORT  zus_sdn_abap_oo_enh_sample.


*----------------------------------------------------------------------*
*       CLASS lcl_super DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_super DEFINITION.
  PUBLIC SECTION.
    DATA: md_message    TYPE bapi_msg.

    METHODS:
      calculate
        IMPORTING
          value(id_val_1) TYPE i
          value(id_val_2) TYPE i
          value(id_val_3) TYPE i,

      display.
ENDCLASS.                    "lcl_super DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_subclass DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_subclass DEFINITION
                   INHERITING FROM lcl_super.

  PUBLIC SECTION.
    METHODS:
      calculate_enhanced
        IMPORTING
          value(id_val_1) TYPE i
          value(id_val_2) TYPE i
          value(id_val_3) TYPE i
          value(id_val_4) TYPE i.
ENDCLASS.                    "lcl_subclass DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_wrapper DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_wrapper DEFINITION.

  PUBLIC SECTION.
    DATA: mo_super    TYPE REF TO lcl_super.

    METHODS:
      constructor,
      calculate
        IMPORTING
          value(id_val_1) TYPE i
          value(id_val_2) TYPE i
          value(id_val_3) TYPE i
          value(id_val_4) TYPE i,

      display.

ENDCLASS.                    "lcl_wrapper DEFINITION



*----------------------------------------------------------------------*
*       CLASS lcl_super IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_super IMPLEMENTATION.

  METHOD calculate.
    me->md_message = 'SUPER CLASS: Method has 3 IMPORTING parameters'.
  ENDMETHOD.                    "calculate

  METHOD display.
    MESSAGE me->md_message TYPE 'I'.
  ENDMETHOD.                    "display

ENDCLASS.                    "lcl_super IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS lcl_subclass IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_subclass IMPLEMENTATION.

  METHOD calculate_enhanced.
    CALL METHOD me->calculate
      EXPORTING
        id_val_1 = id_val_1
        id_val_2 = id_val_2
        id_val_3 = id_val_3.

    me->md_message = 'SUB CLASS: Method has 4 IMPORTING parameters'.
  ENDMETHOD.                    "calculate_enhanced

ENDCLASS.                    "lcl_subclass IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS lcl_wrapper IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_wrapper IMPLEMENTATION.

  METHOD constructor.
    CREATE OBJECT me->mo_super.
  ENDMETHOD.                    "constructor

  METHOD calculate.
    CALL METHOD me->mo_super->calculate
      EXPORTING
        id_val_1 = id_val_1
        id_val_2 = id_val_2
        id_val_3 = id_val_3.

    me->mo_super->md_message = 'WRAPPER CLASS: Method has 4 IMPORTING parameters'.

  ENDMETHOD.                    "calculate

  METHOD display.
    me->mo_super->display( ).
  ENDMETHOD.                    "display

ENDCLASS.                    "lcl_wrapper IMPLEMENTATION


DATA: go_super    TYPE REF TO lcl_super,
      go_sub      TYPE REF TO lcl_subclass,
      go_wrapper  TYPE REF TO lcl_wrapper.

START-OF-SELECTION.

  CREATE OBJECT: go_super,
                 go_sub,
                 go_wrapper.

  " Genuine method
  CALL METHOD go_super->calculate
    EXPORTING
      id_val_1 = 1
      id_val_2 = 2
      id_val_3 = 3.
  go_super->display( ).


  " Subclass
  CALL METHOD go_sub->calculate  " inherited method
    EXPORTING
      id_val_1 = 1
      id_val_2 = 2
      id_val_3 = 3.
**      id_val_4 = 4. This parameter could be implemented using enhancement
  go_sub->display( ).

  CALL METHOD go_sub->calculate_enhanced
    EXPORTING
      id_val_1 = 1
      id_val_2 = 2
      id_val_3 = 3
      id_val_4 = 4.
  go_sub->display( ).

  " Wrapper class
  CALL METHOD go_wrapper->calculate
    EXPORTING
      id_val_1 = 1
      id_val_2 = 2
      id_val_3 = 3
      id_val_4 = 4.
  go_wrapper->display( ).



END-OF-SELECTION.

Regards

Uwe

5 REPLIES 5

GrahamRobbo
Active Contributor
0 Kudos

Hi Myah,

some options of the top of my head....not all possibilities by any means....

Option 1. Use the enhancement framework to add the importing parameter to the method and to enhance the method. Note it can only be defined as an optional parameter.

Option 2. Create a subclass and add a new method that takes the four importing parameters. This method should call the old method to reuse all it's code, i.e. super->oldmethod. This means callers need to change the way they call the method. It also assumes we are talking about a class that can be subclassed.

Option 3. Create a new class that "wraps" the old class. This class instantiates the old class and reuses as much of it as possible. Again callers will need to be modified to call the new class.

Cheers

Graham Robbo

0 Kudos

Hi Graham Robinson,

Thanks for your prompt reply. Unfortunately, I could only understand it partially.

It would greatly appreciated if you could provide me some example in code for my better understanding.

Thanks.

0 Kudos

Hi Myah,

code samples wouldn't be very helpful.

I suggest you read up on the Enhancement Framework first as this would be the first option I would investigate. There are blogs on this and of course there is always the SAP documentation.

The other options I mentioned are just standard OO programming concepts.

Cheers

Graham Robbo

uwe_schieferstein
Active Contributor
0 Kudos

Hello Myah

Options (2) and (3) are demonstrated in sample report ZUS_SDN_ABAP_OO_ENH_SAMPLE.


*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_ABAP_OO_ENH_SAMPLE
*&
*&---------------------------------------------------------------------*
*& Thread: Method parameters
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1063590"></a>
*&---------------------------------------------------------------------*

REPORT  zus_sdn_abap_oo_enh_sample.


*----------------------------------------------------------------------*
*       CLASS lcl_super DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_super DEFINITION.
  PUBLIC SECTION.
    DATA: md_message    TYPE bapi_msg.

    METHODS:
      calculate
        IMPORTING
          value(id_val_1) TYPE i
          value(id_val_2) TYPE i
          value(id_val_3) TYPE i,

      display.
ENDCLASS.                    "lcl_super DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_subclass DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_subclass DEFINITION
                   INHERITING FROM lcl_super.

  PUBLIC SECTION.
    METHODS:
      calculate_enhanced
        IMPORTING
          value(id_val_1) TYPE i
          value(id_val_2) TYPE i
          value(id_val_3) TYPE i
          value(id_val_4) TYPE i.
ENDCLASS.                    "lcl_subclass DEFINITION


*----------------------------------------------------------------------*
*       CLASS lcl_wrapper DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_wrapper DEFINITION.

  PUBLIC SECTION.
    DATA: mo_super    TYPE REF TO lcl_super.

    METHODS:
      constructor,
      calculate
        IMPORTING
          value(id_val_1) TYPE i
          value(id_val_2) TYPE i
          value(id_val_3) TYPE i
          value(id_val_4) TYPE i,

      display.

ENDCLASS.                    "lcl_wrapper DEFINITION



*----------------------------------------------------------------------*
*       CLASS lcl_super IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_super IMPLEMENTATION.

  METHOD calculate.
    me->md_message = 'SUPER CLASS: Method has 3 IMPORTING parameters'.
  ENDMETHOD.                    "calculate

  METHOD display.
    MESSAGE me->md_message TYPE 'I'.
  ENDMETHOD.                    "display

ENDCLASS.                    "lcl_super IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS lcl_subclass IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_subclass IMPLEMENTATION.

  METHOD calculate_enhanced.
    CALL METHOD me->calculate
      EXPORTING
        id_val_1 = id_val_1
        id_val_2 = id_val_2
        id_val_3 = id_val_3.

    me->md_message = 'SUB CLASS: Method has 4 IMPORTING parameters'.
  ENDMETHOD.                    "calculate_enhanced

ENDCLASS.                    "lcl_subclass IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS lcl_wrapper IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_wrapper IMPLEMENTATION.

  METHOD constructor.
    CREATE OBJECT me->mo_super.
  ENDMETHOD.                    "constructor

  METHOD calculate.
    CALL METHOD me->mo_super->calculate
      EXPORTING
        id_val_1 = id_val_1
        id_val_2 = id_val_2
        id_val_3 = id_val_3.

    me->mo_super->md_message = 'WRAPPER CLASS: Method has 4 IMPORTING parameters'.

  ENDMETHOD.                    "calculate

  METHOD display.
    me->mo_super->display( ).
  ENDMETHOD.                    "display

ENDCLASS.                    "lcl_wrapper IMPLEMENTATION


DATA: go_super    TYPE REF TO lcl_super,
      go_sub      TYPE REF TO lcl_subclass,
      go_wrapper  TYPE REF TO lcl_wrapper.

START-OF-SELECTION.

  CREATE OBJECT: go_super,
                 go_sub,
                 go_wrapper.

  " Genuine method
  CALL METHOD go_super->calculate
    EXPORTING
      id_val_1 = 1
      id_val_2 = 2
      id_val_3 = 3.
  go_super->display( ).


  " Subclass
  CALL METHOD go_sub->calculate  " inherited method
    EXPORTING
      id_val_1 = 1
      id_val_2 = 2
      id_val_3 = 3.
**      id_val_4 = 4. This parameter could be implemented using enhancement
  go_sub->display( ).

  CALL METHOD go_sub->calculate_enhanced
    EXPORTING
      id_val_1 = 1
      id_val_2 = 2
      id_val_3 = 3
      id_val_4 = 4.
  go_sub->display( ).

  " Wrapper class
  CALL METHOD go_wrapper->calculate
    EXPORTING
      id_val_1 = 1
      id_val_2 = 2
      id_val_3 = 3
      id_val_4 = 4.
  go_wrapper->display( ).



END-OF-SELECTION.

Regards

Uwe

0 Kudos

Hi Uwe and Graham,

Thanks for the input and patient. Both of you are so helpful. I have the ideas now .