Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Method parameters

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.

replied

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 View this answer in context
Not what you were looking for? View more on this topic or Ask a question