Mentor SAP
2017-10-11 Submitted by:- Admin

Final Method cannot be redefined. If we declare a method with the FINAL statement then that method cannot be declared with REDEFINITION. In this case, the syntax error will come at the time of compilation as mentioned below:

 

Below we have created another version of the program where we have changed the name of the method in the child class. That's mean here the program has two different methods in parent and child class. Being a child/sub class it is inheriting all the data and methods from parent class.

REPORT zinherit_final_method.

*----------------------------------------------------------------------*
*       CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
  PUBLIC SECTION.
    DATA v_txt TYPE char50.
    METHODS m_par FINAL.
ENDCLASS.                    "parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
  METHOD m_par.
    v_txt = 'Final Method in Parent Class'.
    WRITE / v_txt.
  ENDMETHOD.                    "m_par
ENDCLASS.                    "parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
  PUBLIC SECTION.
    METHODS m_chi FINAL.
ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD m_chi.
    CALL METHOD m_par.
    v_txt = 'Final Method in Child Class'.
    WRITE / v_txt.
  ENDMETHOD.                    "m_chi
ENDCLASS.                    "child IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj_chi TYPE REF TO child.
  CREATE OBJECT obj_chi.
  CALL METHOD obj_chi->m_chi.

 

Output:-