Mentor SAP
2018-02-02 Submitted by:- Admin

When a subclass contains new components which are not declared in the superclass then calling the new components by creating a dynamic reference of superclass will cause the syntax error in the system.

Here we have defined a parent class and a child class inheriting from the parent class. In the parent class, we have declared a method m_par. And in the child class, we have redefined the method m_par and a new method m_chi. Now we have declared the object obj_par referenced by parent and object obj_chi referenced by child statically. At the time of object creation, we have referenced obj_par of type child by dynamically. Now, whenever we call the method m_chi by obj_par then the syntax error will come. Here the error comes because the method m_chi is unknown to parent class though obj_par is dynamically referenced to the child class. Hence the superclass object obj_par has found the method m_chi a new component which is not declared in the superclass.

 

Below we have defined a modified version of the program where we have created the static reference of parent and child class. Then we have called the method from superclass. After that, we have called the same but redefined method from subclass by creating the dynamic reference of the superclass.
*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
  PUBLIC SECTION.
    DATA v_txt TYPE char50.
    METHODS m_par.
ENDCLASS.                    "parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
  PUBLIC SECTION.
    METHODS m_par REDEFINITION.
    METHODS m_chi.
ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*
*       CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
  METHOD m_par.
    v_txt = 'Parent Class Method'.
    WRITE / v_txt.
  ENDMETHOD.                    "m_par
ENDCLASS.                    "parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD m_par.
    v_txt = 'Parent Class Method from Child Class'.
    WRITE / v_txt.
  ENDMETHOD.                    "m_par

  METHOD m_chi.
    v_txt = 'Child Class Method'.
    WRITE / v_txt.
  ENDMETHOD.                    "m_chi
ENDCLASS.                    "child IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj_par TYPE REF TO parent, "Static Type
        obj_chi TYPE REF TO child.  "Static Type

  CREATE OBJECT obj_par.
  CALL METHOD   obj_par->m_par. "Call the method from Parent class
  SKIP.

  CREATE OBJECT: obj_par TYPE child,
                 obj_chi.
  CALL METHOD:   obj_par->m_par, "Call the Redefinition of m_par
                 obj_chi->m_chi.
The following is the Output: