When a sub class contains new components which are not declared in the super class then calling the new components by creating a dynamic reference of super class will cause syntax error in the system.
Here we have defined a parent class and a child class inheriting from 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 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 child class. Hence the super class object obj_par has found the method m_chi a new component which is not declared in the super class.
*&---------------------------------------------------------------------*
*& 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.
Here we have defined a parent class and a child class inheriting from 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 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 child class. Hence the super class object obj_par has found the method m_chi a new component which is not declared in the super class.
Below we have defined a modified version of the program where we have created static reference of parent and child class. Then we have called method from super class. After that we have called the same but redefined method from sub class by creating the dynamic reference of the super class.
*&---------------------------------------------------------------------*
*& 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:
1 comment:
SAP Success Factors Real Time Hands on Training in Chennai...
Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..
http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286
Both Classroom/Online Training is Available!!!!!!
Post a Comment