Abstract method can only be implemented in any of sub classes of the parent class by using REDEFINITION of the method. If we try to implement any abstract method then syntax error will come as follows:
We can implement the abstract method in any child or sub class by defining the method with REDEFINITION. Below is the same program but we have implemented the abstract method in the child class inherited from the parent. Before implementation we have defined the method by REDEFINITION. In this way we can use an abstract method in any program.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION ABSTRACT.
PUBLIC SECTION.
DATA v_abs TYPE char50.
METHODS m_abs ABSTRACT.
ENDCLASS. "parent DEFINITION
*----------------------------------------------------------------------*
* CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
ENDCLASS. "parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
PUBLIC SECTION.
METHODS m_abs REDEFINITION.
ENDCLASS. "child DEFINITION
*----------------------------------------------------------------------*
* CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
METHOD m_abs.
v_abs = 'Abstract Method is implemented in Child class'.
WRITE: / v_abs,
/ 'by using REDEFINITION of Method'.
ENDMETHOD. "m_abs
ENDCLASS. "child IMPLEMENTATION
START-OF-SELECTION.
DATA obj_chi TYPE REF TO child.
CREATE OBJECT obj_chi.
CALL METHOD obj_chi->m_abs.
Below is the Output:
We can implement the abstract method in any child or sub class by defining the method with REDEFINITION. Below is the same program but we have implemented the abstract method in the child class inherited from the parent. Before implementation we have defined the method by REDEFINITION. In this way we can use an abstract method in any program.
*&---------------------------------------------------------------------*
*& Report ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zsr_test.
*----------------------------------------------------------------------*
* CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION ABSTRACT.
PUBLIC SECTION.
DATA v_abs TYPE char50.
METHODS m_abs ABSTRACT.
ENDCLASS. "parent DEFINITION
*----------------------------------------------------------------------*
* CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
ENDCLASS. "parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
PUBLIC SECTION.
METHODS m_abs REDEFINITION.
ENDCLASS. "child DEFINITION
*----------------------------------------------------------------------*
* CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
METHOD m_abs.
v_abs = 'Abstract Method is implemented in Child class'.
WRITE: / v_abs,
/ 'by using REDEFINITION of Method'.
ENDMETHOD. "m_abs
ENDCLASS. "child IMPLEMENTATION
START-OF-SELECTION.
DATA obj_chi TYPE REF TO child.
CREATE OBJECT obj_chi.
CALL METHOD obj_chi->m_abs.
Below is the Output:
No comments:
Post a Comment