Monday, March 25, 2013

Redefinition of Method by Child or Sub Class


Subclass can redefine the inherited methods and can change its functionality as per requirement. Here we have to declare the methods in child class like this - METHODS METH REDEFINITION.

We have a program where we have defined 2 classes - Parent and Child classes. In the Parent class we have declared data and method in the Public section as well as Protected section. Next we have implemented these two methods as well. Then we have declared the child class inheriting from parent class. There we have declared the same methods in Public and Protected section. The only difference is that we have Redefined these methods by the statement REDEFINITION. Here at the time of implementation we have changed the functionality of those methods as well. Now in the start of selection we just call the methods by instantiating the classes. In the output we can have the reflection of parent function as well as child function also.

REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS cl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent DEFINITION.
  PUBLIC SECTION.
    DATA v_par_pub TYPE char40.
    METHODS m_test_pub.

  PROTECTED SECTION.
    DATA v_par_pro TYPE char40.
    METHODS m_test_pro.
ENDCLASS.                    "cl_parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent IMPLEMENTATION.
  METHOD m_test_pub.
    v_par_pub = 'Parent class Public data'.
    CALL METHOD m_test_pro.

    "Protected method cannot be called from user level
    WRITE: / v_par_pub,
           / v_par_pro.
  ENDMETHOD.                    "m_test_pub

  METHOD m_test_pro.
    v_par_pro = 'Parent class Protected data'.
  ENDMETHOD.                    "m_test_pro
ENDCLASS.                    "cl_parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cl_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child DEFINITION INHERITING FROM cl_parent.
  PUBLIC SECTION.
    DATA v_chi_pub TYPE char40.

    "Same method of parent class is redefined
    METHODS m_test_pub REDEFINITION.

  PROTECTED SECTION.
    DATA v_chi_pro TYPE char40.

    "Same method of parent class is redefined
    METHODS m_test_pro REDEFINITION.
ENDCLASS.                    "cl_child DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child IMPLEMENTATION.
  METHOD m_test_pub.
    v_chi_pub = 'Child class Public data'.
    CALL METHOD m_test_pro.

    "Protected method cannot be called from user level
    WRITE: / v_chi_pub,
           / v_chi_pro.
  ENDMETHOD.                    "m_test_pub

  METHOD m_test_pro.
    v_chi_pro = 'Child class Protected data'.
  ENDMETHOD.                    "m_test_pro
ENDCLASS.                    "cl_child IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj_par TYPE REF TO cl_parent,
        obj_chi TYPE REF TO cl_child.
  CREATE OBJECT: obj_par,
                 obj_chi.

  "Calling the public methods only
  CALL METHOD: obj_par->m_test_pub.
  SKIP.
  CALL METHOD: obj_chi->m_test_pub.


Below is the Output:


5 comments:

yektek training said...

Very nicepost thanks swathi

Unknown said...

What is the utility of using Redefinition? In the last program we saw that the same we can achive(i.e. using child class we implemented the method of Parent class) without using Redefinition.

simashree said...

Thank you for such guidance about astrology,Best Blog on
vasiyam in chennai

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

Camelliacanan said...

wow really nice. It will be helpful for the people those who are ready to crack the interview and please also for remind what they have learned throughout concept.

SAP HR Training in Chennai

SAP SD Training in Chennai

SAP Basis Training in Chennai