Using inheritance in SAP OOABAP

Working with inheritance in SAP classes, using inheritance in SAP classes

+ -
Go to SE24, provide name as ZCL_SAPN_SUPERCLASS, create

Using inheritance in SAP classes

A pop up will open provide description, un-select final check box, save and save as local object.

Using inheritance in SAP classes

Go to methods tab, provide method name as GET_MATERIAL_DETAILS-INSTANCE-PUBLIC.

Using inheritance in SAP classes

Go to parameters tab and add below paramaters.

Using inheritance in SAP classes

Go back to methods, double click on method name and add below code.

Using inheritance in SAP classes

  METHOD GET_MATERIAL_DETAILS.
    
    SELECT SINGLE * FROM MARA
      INTO EX_MARA
      WHERE MATNR = IM_MATNR.
      
  ENDMETHOD.
Save and activate the class.

Super class is created, now we have to inherit this class and derive a new class(child class).

Go to SE24, provide name as ZCL_SAPN_CHILDCLASS, create.

Using inheritance in SAP classes

A pop up will open provide description, click on create inheritance icon(see below image), provide super class name.

Using inheritance in SAP classes

Using inheritance in SAP classes

Save, save as a local object, now you can see the inherited method, using inheritance we can overwrite the method implementation to do this click on redifinition icon (see image below)

Using inheritance in SAP classes

We will add additional functionality to get material description details along with material details, go to attributes tab and add a attribute ls_makt-instance-public-makt.

Using inheritance in SAP classes

Go back to methods, doublle click on method, uncomment the inherited code, pass parameters and add additional code to get material descriptions.

Using inheritance in SAP classes

  METHOD GET_MATERIAL_DETAILS.
    
    CALL METHOD SUPER->GET_MATERIAL_DETAILS
      EXPORTING
        IM_MATNR = IM_MATNR
      IMPORTING
        EX_MARA  = EX_MARA.
***Additional code to get material descriptions
    SELECT * FROM MAKT
      INTO LS_MAKT
      WHERE MATNR = EX_MARA-MATNR.
    ENDSELECT.
    
  ENDMETHOD.

Using Class inprogram

Create a program in SE38 and follow below steps(code).
REPORT ZSAPN_CLASS_INHERITANCE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CHILDCLASS. "declare class
DATA : WA_MARA TYPE MARA. "declare MARA
DATA WA_MAKT TYPE MAKT. "declare MAKT

PARAMETERS P_MATNR TYPE MARA-MATNR. "material input
CREATE OBJECT LO_CLASS. "create instance for class

START-OF-SELECTION.
  CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "get material details
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MARA  = WA_MARA.
  
  WA_MAKT = LO_CLASS->LS_MAKT. "access material description from class attribute
  WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL. "print material details
  WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX. "print material descriptions

Learner Questions


No Questions by learners, be first one to ask ..!!

    Please Sign in to ask a question

    Was this lesson helpful to you? Yes No 52 People out of 56 think this lesson helpful


    Lesson Navigation

    ←Previous Chapter
    Next Lesson → Local Classes in SAP OOABAP