Sunday, September 2, 2018

Inheritance in OOABAP simple example

INHERITANCE :
One of the most important concepts in object oriented programming is that of inheritance(common in all oops programming language). Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.
  • An object of one class can acquire the properties of another class.
  • Derived class inherits the data and methods of a super class. However, they can overwrite methods and also add new methods.
 SYNTAX:---
CLASS <subclass> DEFINITION INHERITING FROM <superclass>.
 
 
 

*&---------------------------------------------------------------------*

*& Report  ZINHERITANCE

*&

*&------------------------------------------------------------------

REPORT  ZINHERITANCE.

PARAMETERSs_matnr type mara-matnr.

DATAwa_mara TYPE mara.

DATAWA_MAKT_TEMP TYPE MAKT.

CLASS SUPER_cLA DEFINITION.

PUBLIC SECTION.

METHODS   GET_MATERIAL

        IMPORTING IM_MATNR TYPE MARA-MATNR

        EXPORTING EX_MARA TYPE MARA.

ENDCLASS.
CLASS SUPER_CLA IMPLEMENTATION.

  METHOD GET_MATERIAL.

    SELECT SINGLE FROM MARA INTO EX_MARA WHERE MATNR IM_MATNR.

  ENDMETHOD.

ENDCLASS.


******* FOR INHERITANCE ALWAYS WRITE INHERITANCE FROM AND 
 BESIDE  METHODS WRITE 
REDIFINITION*****
CLASS CHILD_CLAS DEFINITION INHERITING FROM SUPER_CLA.

  PUBLIC SECTION.

DATA wa_makt type makt.

METHODS :  GET_MATERIAL REDEFINITION"INHERITANING USING REDEFINTION"



ENDCLASS.

********CHILD CLASS IMPLEMENTATION******************
CLASS CHILD_cLAS IMPLEMENTATION.

METHODGET_MATERIAL.

  CALL METHOD SUPER->GET_MATERIAL "CALL METHOD TO GET MATERIAL

      EXPORTING

        IM_MATNR S_MATNR

      IMPORTING

        EX_MARA EX_MARA.

SELECT SINGLE from makt INTO WA_MAKT WHERE MATNR WA_MARA-MATNR.ENDMETHOD.

ENDCLASS.

START-OF-SELECTION. 
DATALO_OBJ TYPE REF TO CHILD_CLAS.

CREATE OBJECT LO_OBJ.      "CREATE OBJECT OF CHILD CLASS"

CALL METHOD LO_OBJ->GET_MATERIAL

EXPORTING

  IM_MATNR S_MATNR

  IMPORTING

       EX_MARA WA_MARA.



  WA_MAKT_TEMP LO_OBJ->WA_MAKT.



WRITE:/' MATERIAL DETAILS    'WA_MARA-MATNR WA_MARA-MTARTWA_MARA-MEINS,
 WA_MARA-MATKL.



WRITE/'MATERIAL DETAILED DESCRIPTION',WA_MAKT_TEMP-MATNR WA_MAKT_TEMP-MAKTX.