Monday, March 25, 2013

Static Attributes exist only Once in Inheritance Tree


Here we have a program where we have defined a class cls1. In the public section we are declaring static data and method. Then we have implemented the class cls1 with the method. The method will write the static data. Next we declare second class cls2 and third class cls3 inheriting from cls1. In the start of selection we give value to the static data and call the static method. Here the point is that we give value to the cls3 class data and we call method of cls2. In spite of this we are getting the same value as an output because cls2 and cls3 are the sub classes of cls1.

Hence we can say that the static attributes exist only once in each Inheritance tree. We can change the value of static data from the outside only one time and that change will reflect to all other sub classes. So they will be visible to other classes in the Inheritance tree. 


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

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA v_txt TYPE char50.
    CLASS-METHODS m_cls1.
ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD m_cls1.
    WRITE / v_txt.
  ENDMETHOD.                                                "m_cls1
ENDCLASS.                    "cls1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION INHERITING FROM cls1.
ENDCLASS.                    "cls2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls3 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls3 DEFINITION INHERITING FROM cls1.
ENDCLASS.                    "cls3 DEFINITION

START-OF-SELECTION.
  cls3=>v_txt = 'SAP ABAP Object Oriented'.
  CALL METHOD cls2=>m_cls1.



Below is the Output:



This similar output can be produced in a different way also. Below is another version of the program and here we have declared 2 methods in the class cls1. In the 2nd method we are giving the value to the static data and then calling the 1st method. In the start of selection we just call the 2nd method by class cls1 and we are getting the similar output. 

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

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA v_txt TYPE char50.
    CLASS-METHODS: m_cls1, m_cls2.
ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD m_cls1.
    WRITE / v_txt.
  ENDMETHOD.                                                "m_cls1

  METHOD m_cls2.
    v_txt = 'SAP ABAP Object Oriented'.
    CALL METHOD m_cls1.
  ENDMETHOD.                                                "m_cls2
ENDCLASS.                    "cls1 IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION INHERITING FROM cls1.
ENDCLASS.                    "cls2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls3 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls3 DEFINITION INHERITING FROM cls1.
ENDCLASS.                    "cls3 DEFINITION

START-OF-SELECTION.
  CALL METHOD cls3=>m_cls2.


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