Tuesday, March 26, 2013

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:

9 comments:

yektek training said...

nice post thamks for sharing from Swathi

Anonymous said...

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.

SANDIP ROY said...

v_text is being modified though it has been declared in parent class.

simashree said...

Thank you for such guidance about astrology,Best Blog on Astrology Services in Chennai

sanayamohit said...

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!!!!!!

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

http://thecreatingexperts.com/sap-mm-training-in-chennai/

suganya said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

sameera sam said...


Thank you for giving this best information. It’s a very nice topic
SAP Success Factor Online Access

gilledashivani said...

Thank you for sharing the great blog. BEST SAP ABAP TRAINING IN HYDERABAD