Monday, March 25, 2013

Final Method cannot be redefined


Final Method cannot be redefined. If we declare a method with FINAL statement then that method cannot be declared with REDEFINITION. In this case 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  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       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.


Below is the Output:


3 comments:

Anonymous said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

Unknown said...


It's so nice article thank you for sharing a valuable content
SAP MDG Online Access

Unknown said...

ood post! Thanks for sharing this Information.The information you Provided is much useful on
sap-hr abap online classes