Gyan Factory

Gyan Factory
SAP Technical Project Support

Sunday, February 14, 2016

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.

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
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

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

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

*----------------------------------------------------------------------*
*       CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
  PUBLIC SECTION.
    METHODS constructor.
ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD constructor.
    CALL METHOD super->constructor.
    v_txt = 'Child Class Constructor'.
    WRITE / v_txt.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "child IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS grandchild DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS grandchild DEFINITION INHERITING FROM child.
  PUBLIC SECTION.
    METHODS constructor.
ENDCLASS.                    "grandchild DEFINITION

*----------------------------------------------------------------------*
*       CLASS grandchild IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS grandchild IMPLEMENTATION.
  METHOD constructor.
    CALL METHOD super->constructor.
    v_txt = 'Grand Child Class Constructor'.
    WRITE / v_txt.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "grandchild IMPLEMENTATION

START-OF-SELECTION.
  DATA obj TYPE REF TO grandchild.
  CREATE OBJECT obj.


Below is the Output:

No comments:

Post a Comment