Tuesday, March 26, 2013

Static type and Dynamic type


Static reference type cannot be changed further so it can point to a super class only. Dynamic reference type can be modified further so it can point to sub classes. Hence a static type of reference can point to super class only whereas a dynamic type of reference can point to any of its sub classes only.

In the following program there two classes - parent & child. Here parent is the dynamic and child is static. Hence we can point parent class into child class. Here the child class is static and always points to parent class.

So, obj_parent3 = obj_child is correct. Because child class (static) can point to parent class and this parent class in pointing to its subclass. But obj_child = obj_parent1 is incorrect. Because parent class (dynamic) can not be converted to its subclass.

REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS cl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent DEFINITION.
  PUBLIC SECTION.
    DATA v_test TYPE char40.
    METHODS m_parent.
ENDCLASS.                    "cl_parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent IMPLEMENTATION.
  METHOD m_parent.
    v_test = 'I am from Parent class'.
    WRITE: /3 v_test.
  ENDMETHOD.                    "m_parent
ENDCLASS.                    "cl_parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cl_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child DEFINITION INHERITING FROM cl_parent.
  PUBLIC SECTION.
    METHODS m_parent REDEFINITION.
ENDCLASS.                    "cl_child DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child IMPLEMENTATION.
  METHOD m_parent.
    v_test = 'I am from Child class'.
    WRITE: /3 v_test.
  ENDMETHOD.                    "m_parent
ENDCLASS.                    "cl_child IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj_parent1 TYPE REF TO cl_parent,
        obj_parent2 TYPE REF TO cl_parent,
        obj_parent3 TYPE REF TO cl_parent,
        obj_child   TYPE REF TO cl_child.

  CREATE OBJECT: obj_parent1,               "Parent class object
                 obj_parent2 TYPE cl_child, "Child class object
                 obj_parent3,               "Parent class object
                 obj_child.                 "Child class object

  obj_parent3 = obj_child. "Parent converted into child class object

  CALL METHOD: obj_parent1->m_parent, "Calling parent method
               obj_parent2->m_parent, "Calling child method
               obj_parent3->m_parent, "Calling child method
               obj_child->m_parent.   "Calling child method
 
 
Below is the output:

1 comment:

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