Sub Class can Modify Constructor of Super Class.
Subclass can modify the constructor method which has already been implemented by the super class. In this modification sub class can add some extra functionality as per requirement. And also the constructor of the super class has to be called by using CALL METHOD SUPER->CONSTRUCTOR statement inside the implementation in sub classes. Here REDEFINITION statement is not needed to modify the constructor. If we call the constructor directly then syntax error will be there.
We have created a program where we have defined a parent class then a child class (Inherited from parent class) and then a grandchild class (Inherited from child class). We have implemented a constructor in parent class and then re implemented it in the child class as well as in the grandchild class. Here we have to call the super class constructor method at first then we can modify the constructor. We have used the statement for calling the super class constructor - CALL METHOD SUPER->CONSTRUCTOR. Output will be coming in the order of Parent->Child->Grandchild. Here we have not used the REDEFINITION statement to modify the constructor.
REPORT zsr_test NO STANDARD PAGE HEADING.
*----------------------------------------------------------------------*
* CLASS cl_grand DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_grand DEFINITION.
PUBLIC SECTION.
DATA v_grand TYPE char40.
METHODS constructor.
ENDCLASS. "cl_grand DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_grand IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_grand IMPLEMENTATION.
METHOD constructor.
v_grand = 'I am from Grand Parent class'.
WRITE: /3 v_grand.
ENDMETHOD. "constructor
ENDCLASS. "cl_grand IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS cl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent DEFINITION INHERITING FROM cl_grand.
PUBLIC SECTION.
METHODS constructor.
ENDCLASS. "cl_parent DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor.
v_grand = 'I am from Parent class'.
WRITE: /3 v_grand.
ENDMETHOD. "constructor
ENDCLASS. "cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS cl_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child DEFINITION INHERITING FROM cl_parent.
PUBLIC SECTION.
METHODS constructor.
ENDCLASS. "cl_child DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor.
v_grand = 'I am from Child class'.
WRITE: /3 v_grand.
ENDMETHOD. "constructor
ENDCLASS. "cl_child IMPLEMENTATION
START-OF-SELECTION. DATA obj_child TYPE REF TO cl_child.
CREATE OBJECT obj_child.
Below is the Output:
We have created a program where we have defined a parent class then a child class (Inherited from parent class) and then a grandchild class (Inherited from child class). We have implemented a constructor in parent class and then re implemented it in the child class as well as in the grandchild class. Here we have to call the super class constructor method at first then we can modify the constructor. We have used the statement for calling the super class constructor - CALL METHOD SUPER->CONSTRUCTOR. Output will be coming in the order of Parent->Child->Grandchild. Here we have not used the REDEFINITION statement to modify the constructor.
REPORT zsr_test NO STANDARD PAGE HEADING.
*----------------------------------------------------------------------*
* CLASS cl_grand DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_grand DEFINITION.
PUBLIC SECTION.
DATA v_grand TYPE char40.
METHODS constructor.
ENDCLASS. "cl_grand DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_grand IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_grand IMPLEMENTATION.
METHOD constructor.
v_grand = 'I am from Grand Parent class'.
WRITE: /3 v_grand.
ENDMETHOD. "constructor
ENDCLASS. "cl_grand IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS cl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent DEFINITION INHERITING FROM cl_grand.
PUBLIC SECTION.
METHODS constructor.
ENDCLASS. "cl_parent DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor.
v_grand = 'I am from Parent class'.
WRITE: /3 v_grand.
ENDMETHOD. "constructor
ENDCLASS. "cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
* CLASS cl_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child DEFINITION INHERITING FROM cl_parent.
PUBLIC SECTION.
METHODS constructor.
ENDCLASS. "cl_child DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child IMPLEMENTATION.
METHOD constructor.
CALL METHOD super->constructor.
v_grand = 'I am from Child class'.
WRITE: /3 v_grand.
ENDMETHOD. "constructor
ENDCLASS. "cl_child IMPLEMENTATION
START-OF-SELECTION. DATA obj_child TYPE REF TO cl_child.
CREATE OBJECT obj_child.
Below is the Output:
Post a Comment