Gyan Factory

Gyan Factory
SAP Technical Project Support

Sunday, February 14, 2016

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.

Here we have created a program and we have 3 classes. Parent class then child class and grandchild class. At the creation of object we can refer the parent class to the child class as well as grandchild class also. But we cannot refer the child class to the parent class. Hence the static reference is the sub classes which can point to the super class. Similarly the dynamic reference is the super class which can point to any of its sub classes.



*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

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

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

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

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD meth.
    v_txt = 'Child Class Method'.
    WRITE / v_txt.
  ENDMETHOD.                    "meth
ENDCLASS.                    "child IMPLEMENTATION

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

*----------------------------------------------------------------------*
*       CLASS grandchild IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS grandchild IMPLEMENTATION.
  METHOD meth.
    v_txt = 'Grandchild Class Method'.
    WRITE / v_txt.
  ENDMETHOD.                    "meth
ENDCLASS.                    "grandchild IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj1    TYPE REF TO parent,"Static Type
        obj11   TYPE REF TO parent,"Static Type
        obj111  TYPE REF TO parent,"Static Type
        obj112  TYPE REF TO parent,"Static Type

        obj2    TYPE REF TO child,"Static Type
        obj22   TYPE REF TO child,"Static Type
        obj222  TYPE REF TO child,"Static Type

        obj3    TYPE REF TO grandchild,"Static Type
        obj33   TYPE REF TO grandchild,"Static Type
        obj333  TYPE REF TO grandchild."Static Type

  CREATE OBJECT: obj1,
                 obj11 TYPE child,"Dynamic Type can point sub class
                 obj111,
                 obj112,

                 "obj2 type parent, Dynamic cannot point Super Class
                 obj22,
                 obj222,

                 obj3,
                 "obj33 type child, Dynamic cannot point Super Class
                 obj333.

  obj111 = obj3."Dynamic Type can point sub class
  "obj22 = obj1. Dynamic cannot point Super Class
  obj222 = obj3."Dynamic Type can point sub class

  CALL METHOD: obj1->meth,   "Call the Parent Class
               obj11->meth,  "Call the Child Class
               obj111->meth, "Call the Grandchild Class
               obj112->meth. "Call the Parent Class
  SKIP 2.
  CALL METHOD: obj22->meth,  "Call the Child Class
               obj222->meth. "Call the Grandchild Class
  SKIP 2.
  CALL METHOD: obj3->meth,   "Call the Grandchild Class
               obj333->meth. "Call the Grandchild Class




Below is the output:

No comments:

Post a Comment