banner ads

Redefinition of Method by Child or Sub Class

Subclass can redefine the inherited methods and can change its functionality as per requirement. Here we have to declare the methods in child class like this - METHODS METH REDEFINITION.

We have a program where we have defined 2 classes - Parent and Child classes. In the Parent class we have declared data and method in the Public section as well as Protected section. Next we have implemented these two methods as well. Then we have declared the child class inheriting from parent class. There we have declared the same methods in Public and Protected section. The only difference is that we have Redefined these methods by the statement REDEFINITION. Here at the time of implementation we have changed the functionality of those methods as well. Now in the start of selection we just call the methods by instantiating the classes. In the output we can have the reflection of parent function as well as child function also.

REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS cl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent DEFINITION.
  PUBLIC SECTION.
    DATA v_par_pub TYPE char40.
    METHODS m_test_pub.

  PROTECTED SECTION.
    DATA v_par_pro TYPE char40.
    METHODS m_test_pro.
ENDCLASS.                    "cl_parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent IMPLEMENTATION.
  METHOD m_test_pub.
    v_par_pub = 'Parent class Public data'.
    CALL METHOD m_test_pro.

    "Protected method cannot be called from user level
    WRITE: / v_par_pub,
           / v_par_pro.
  ENDMETHOD.                    "m_test_pub

  METHOD m_test_pro.
    v_par_pro = 'Parent class Protected data'.
  ENDMETHOD.                    "m_test_pro
ENDCLASS.                    "cl_parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cl_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child DEFINITION INHERITING FROM cl_parent.
  PUBLIC SECTION.
    DATA v_chi_pub TYPE char40.

    "Same method of parent class is redefined
    METHODS m_test_pub REDEFINITION.

  PROTECTED SECTION.
    DATA v_chi_pro TYPE char40.

    "Same method of parent class is redefined
    METHODS m_test_pro REDEFINITION.
ENDCLASS.                    "cl_child DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child IMPLEMENTATION.
  METHOD m_test_pub.
    v_chi_pub = 'Child class Public data'.
    CALL METHOD m_test_pro.

    "Protected method cannot be called from user level
    WRITE: / v_chi_pub,
           / v_chi_pro.
  ENDMETHOD.                    "m_test_pub

  METHOD m_test_pro.
    v_chi_pro = 'Child class Protected data'.
  ENDMETHOD.                    "m_test_pro
ENDCLASS.                    "cl_child IMPLEMENTATION

START-OF-SELECTION.  DATA: obj_par TYPE REF TO cl_parent,
        obj_chi TYPE REF TO cl_child.
  CREATE OBJECT: obj_par,
                 obj_chi.

  "Calling the public methods only
  CALL METHOD: obj_par->m_test_pub.
  SKIP.
  CALL METHOD: obj_chi->m_test_pub.


Below is the Output:


No comments