SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Inheritance - Static attributes exist only once per inheritance tree

Inheritance - Static attributes exist only once per inheritance tree

Static attributes only exist once in each inheritance tree. One can change them from outside the class using the class component selector with any class name, or within any class in which they are shared. They are visible in all classes in the inheritance tree.

Class C1 contains static attribute , NUM.

Class C2 and C3 are subclasses of class C1.

In the START-OF-SELECTION block, the static attribute , NUM is changed using reference of class C3.

It gets changed with reference to class C2 also.

Hence, static attribute, NUM, changed via class C3 is also changed with respect to C2 also. This demonstrates the theme.

 


REPORT zstp_oo_abap.

CLASS c1 DEFINITION .
  PUBLIC SECTION.
    CLASS-DATA : num TYPE i.
ENDCLASS.

CLASS c1 IMPLEMENTATION .
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
ENDCLASS.

CLASS c3 DEFINITION INHERITING FROM c1.
ENDCLASS.


START-OF-SELECTION.
  c3=>num = 10.
  WRITE:/5 c2=>num.

Output -

image

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in