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
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
PUBLIC SECTION.
DATA p_txt TYPE char50.
METHODS m_par_pub.
PROTECTED SECTION.
DATA date TYPE sy-datum.
METHODS m_par_pro.
ENDCLASS. "parent DEFINITION
*----------------------------------------------------------------------*
* CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
METHOD m_par_pub.
p_txt = 'Method declared in Public of Parent Class'.
WRITE / p_txt.
CALL METHOD m_par_pro.
ENDMETHOD. "m_par_pub
METHOD m_par_pro.
date = sy-datum.
WRITE: / 'Today is = ', date DD/MM/YYYY.
SKIP 2.
ENDMETHOD. "m_par_pro
ENDCLASS. "parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
PUBLIC SECTION.
DATA c_txt TYPE char50.
METHODS m_par_pub REDEFINITION.
PROTECTED SECTION.
DATA time TYPE sy-uzeit.
METHODS m_par_pro REDEFINITION.
ENDCLASS. "child DEFINITION
*----------------------------------------------------------------------*
* CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
METHOD m_par_pub.
c_txt = 'Method Redefined in Public of Child Class'.
WRITE / c_txt.
CALL METHOD m_par_pro.
ENDMETHOD. "m_par_pub
METHOD m_par_pro.
time = sy-uzeit.
WRITE: / 'Now the Time is = ', time.
ENDMETHOD. "m_par_pro
ENDCLASS. "child IMPLEMENTATION
START-OF-SELECTION.
DATA: obj_par TYPE REF TO parent,
obj_chi TYPE REF TO child.
CREATE OBJECT: obj_par, obj_chi.
CALL METHOD: obj_par->m_par_pub,
obj_chi->m_par_pub.
Below is the Output:
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
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
PUBLIC SECTION.
DATA p_txt TYPE char50.
METHODS m_par_pub.
PROTECTED SECTION.
DATA date TYPE sy-datum.
METHODS m_par_pro.
ENDCLASS. "parent DEFINITION
*----------------------------------------------------------------------*
* CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
METHOD m_par_pub.
p_txt = 'Method declared in Public of Parent Class'.
WRITE / p_txt.
CALL METHOD m_par_pro.
ENDMETHOD. "m_par_pub
METHOD m_par_pro.
date = sy-datum.
WRITE: / 'Today is = ', date DD/MM/YYYY.
SKIP 2.
ENDMETHOD. "m_par_pro
ENDCLASS. "parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
PUBLIC SECTION.
DATA c_txt TYPE char50.
METHODS m_par_pub REDEFINITION.
PROTECTED SECTION.
DATA time TYPE sy-uzeit.
METHODS m_par_pro REDEFINITION.
ENDCLASS. "child DEFINITION
*----------------------------------------------------------------------*
* CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
METHOD m_par_pub.
c_txt = 'Method Redefined in Public of Child Class'.
WRITE / c_txt.
CALL METHOD m_par_pro.
ENDMETHOD. "m_par_pub
METHOD m_par_pro.
time = sy-uzeit.
WRITE: / 'Now the Time is = ', time.
ENDMETHOD. "m_par_pro
ENDCLASS. "child IMPLEMENTATION
START-OF-SELECTION.
DATA: obj_par TYPE REF TO parent,
obj_chi TYPE REF TO child.
CREATE OBJECT: obj_par, obj_chi.
CALL METHOD: obj_par->m_par_pub,
obj_chi->m_par_pub.
Below is the Output:
No comments:
Post a Comment