Abstract Method

Abstract Method


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 NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS cl_abs DEFINITION
*----------------------------------------------------------------------*
*       This Abstract class contains abstract & normal methods
*----------------------------------------------------------------------*
CLASS cl_abs DEFINITION ABSTRACT.
  PUBLIC SECTION.
    METHODS: m_normal,
             m_abs ABSTRACT. "It will not be implemented
ENDCLASS.                    "cl_abs DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_abs IMPLEMENTATION
*----------------------------------------------------------------------*
*       Only the normal method will be implemented here
*----------------------------------------------------------------------*
CLASS cl_abs IMPLEMENTATION.
  METHOD m_normal.
    WRITE: /3 'Normal Method in Abstract class'.
  ENDMETHOD.                    "m_normal
ENDCLASS.                    "cl_abs IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cl_normal DEFINITION - Concrete Class
*----------------------------------------------------------------------*
*       Creating child class to implement the abstract method
*----------------------------------------------------------------------*
CLASS cl_normal DEFINITION INHERITING FROM cl_abs.
  PUBLIC SECTION.
    METHODS: m_abs REDEFINITION"Since it has already been declared
             m_child.
ENDCLASS.                    "cl_normal DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_normal IMPLEMENTATION
*----------------------------------------------------------------------*
*       The predefined abstract method is implemented here
*----------------------------------------------------------------------*
CLASS cl_normal IMPLEMENTATION.
  METHOD m_abs. "Previously defined Abstract Method
    WRITE: /3 'Abstract method from child of Abstract class'.
    CALL METHOD m_normal.
    SKIP.
  ENDMETHOD.                    "m_abs

  METHOD m_child.
    WRITE: /3 'Normal child method'.
  ENDMETHOD.                    "m_child
ENDCLASS.                    "cl_normal IMPLEMENTATION

START-OF-SELECTION.  DATA: obj_normal TYPE REF TO cl_normal.

  CREATE OBJECT obj_normal.       "Instantiating the Concrete class
  CALL METHOD: obj_normal->m_abs,
               obj_normal->m_child.

Below is the Output:

Share this

Related Posts