Using inheritance in local classes in SAP ABAP

Concept if inheritance, using inheritance in local class using SAP ABAP programming language

+ -

Final class

Final class is a class property which dosen`t allow to inherite, final class can be specified by using keyword FINAL
Example:
CLASS CL_PARENT_CLASS DEFINITION FINAL . "Final class can use for inheritance
  PUBLIC SECTION.
*METHODS, EVENTS, ATTRIUTES
ENDCLASS.
Final class can not be used for inheritance.

Example of using inheritance in local classes

In the below example we are converting example explained in lesson Using inheritance in SAP Classes into local classes.Refer the lessons side-by-side for better understanding.
Define a parent class.
CLASS CL_PARENT_CLASS DEFINITION FINAL .
  PUBLIC SECTION.
    METHODS : GET_MATERIAL_DETAILS
     IMPORTING IM_MATNR TYPE MARA-MATNR
     EXPORTING EX_MARA TYPE MARA.
ENDCLASS.
Implement parent class.
CLASS CL_PARENT_CLASS IMPLEMENTATION.
  METHOD : GET_MATERIAL_DETAILS.
    SELECT SINGLE * FROM MARA
      INTO EX_MARA
      WHERE MATNR = IM_MATNR.
  ENDMETHOD.
ENDCLASS.               "cl_parent_class
INHERITING FROM is a keyword which is used to derive a class by using inheritance.
REDEFINITION is a keyword which is used to overwrite paraent class method. Define child class

Include an attribute wa_makt to get material description details by using redefinition.

CLASS CL_CHILD_CLASS DEFINITION INHERITING FROM CL_PARENT_CLASS. "inheritance
  PUBLIC SECTION.
    DATA : WA_MAKT TYPE MAKT. "attribute for material descriptions 
    METHODS : GET_MATERIAL_DETAILS REDEFINITION. "we will add additional code
ENDCLASS.
Implement child class.
SUPER is a keyword which is used to access parent class methods in child classes.
CLASS CL_CHILD_CLASS IMPLEMENTATION.
  METHOD: GET_MATERIAL_DETAILS.
    CALL METHOD SUPER->GET_MATERIAL_DETAILS
      EXPORTING
        IM_MATNR = P_MATNR
      IMPORTING
        EX_MARA  = WA_MARA.
    SELECT * FROM MAKT
      INTO WA_MAKT
      WHERE MATNR = WA_MARA-MATNR AND SPRAS = 'E'.
    ENDSELECT.
  ENDMETHOD.
ENDCLASS.               "cl_child_class

The final code will be

Plese try to understand step by step, if you face any difficulty....plese comment at the bottom.
REPORT ZSAPN_LOCAL_CLASS_INHERITANCE.
CLASS CL_CHILD_CLASS DEFINITION DEFERRED. "definition deferred
DATA : LO_CHILD TYPE REF TO CL_CHILD_CLASS. "declare class
DATA : WA_MARA TYPE MARA. "declare work area to store mara data
DATA : WA_MAKT_TMP TYPE MAKT. "declare work area to store makt data
PARAMETERS P_MATNR TYPE MARA-MATNR. "parameter input field for material no
CLASS CL_PARENT_CLASS DEFINITION . "parent class definition not a final class
  PUBLIC SECTION.
    METHODS : GET_MATERIAL_DETAILS "method to get material details
     IMPORTING IM_MATNR TYPE MARA-MATNR
     EXPORTING EX_MARA TYPE MARA.
ENDCLASS.

CLASS CL_CHILD_CLASS DEFINITION INHERITING FROM CL_PARENT_CLASS. "child class using inheritance
  PUBLIC SECTION.
    DATA : WA_MAKT TYPE MAKT. "local attribute to get material descriptions
    METHODS : GET_MATERIAL_DETAILS REDEFINITION. "inherited method using redefinition
ENDCLASS.

START-OF-SELECTION.
  CREATE OBJECT LO_CHILD. "Create object for child class

  CALL METHOD LO_CHILD->GET_MATERIAL_DETAILS "call method to get material details
    EXPORTING
      IM_MATNR = P_MATNR
    IMPORTING
      EX_MARA  = WA_MARA.
  WA_MAKT_TMP = LO_CHILD->WA_MAKT. "material descriptions 

  WRITE:/ 'Material Details:', WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
  WRITE:/ 'Material Descriptions:', WA_MAKT_TMP-MATNR, WA_MAKT_TMP-MAKTX.

CLASS CL_PARENT_CLASS IMPLEMENTATION. "parent class implementation

  METHOD : GET_MATERIAL_DETAILS.
    SELECT SINGLE * FROM MARA
      INTO EX_MARA
      WHERE MATNR = IM_MATNR.

  ENDMETHOD.
ENDCLASS.               "cl_parent_class
CLASS CL_CHILD_CLASS IMPLEMENTATION. "local class implementation
  METHOD: GET_MATERIAL_DETAILS.
    CALL METHOD SUPER->GET_MATERIAL_DETAILS
      EXPORTING
        IM_MATNR = P_MATNR
      IMPORTING
        EX_MARA  = WA_MARA.
    SELECT * FROM MAKT
      INTO WA_MAKT
      WHERE MATNR = WA_MARA-MATNR AND SPRAS = 'E'.
    ENDSELECT.
  ENDMETHOD.
ENDCLASS.               "cl_child_class

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 20 People out of 22 think this lesson helpful


    Lesson Navigation

    Interfaces in local classes in SAP OOABAP← Previous Lesson
    Next Lesson → Developing ALV using OOABAP