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:
4 comments:
nice post thamks for sharing from Swathi
its not like redefining the constructor. Usually every class can have constructor. So you are just calling super class constructor with in child class constructor.
v_text is being modified though it has been declared in parent class.
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