Thursday, August 18, 2011

ATTRIBUTES in CLASS


Attributes can take on values within an object at runtime. The sum of all attributes and their values describes the state of an object.
Attributes can be defined as instance dependent as well as Class dependent. Class attributes (Class attributes are also called static attributes.) are not tied to a single instance, rather they "belong" to all instances of the Class. These attributes exist only once in main memory. Instance-dependent attributes exist once per instance and are tied to a single instance.
In ABAP Objects you differentiate between instance-dependent and class-dependent attributes by means of the ABAP keywords DATA or CLASS-DATA to be used in the definition part:  
REPORT  Z_001_SIMPLE_CLASS.
TYPES: TY_NAME(50) TYPE C.
*----------------------------------------------------------------
*       CLASS CL_NAME DEFINITION
*----------------------------------------------------------------
CLASS CL_NAME DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA I_COUNT TYPE I.   " Class Dependent Attribute
                       " Will have same value in all objects
    DATA NAME TYPE TY_NAME.      " Instance Dependent Attribute
              " Will have different values in different objects
ENDCLASS.                    "CL_NAME DEFINITION
*----------------------------------------------------------------
*       CLASS CL_NAME IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_NAME IMPLEMENTATION.
       " We will use this portion in next examples.
ENDCLASS.                    "CL_NAME IMPLEMENTATION
" CREATE REFERENCE VARIABLES
DATA: INSTANCE1 TYPE REF TO CL_NAME,
      INSTANCE2 TYPE REF TO CL_NAME,
      INSTANCE3 TYPE REF TO CL_NAME.
START-OF-SELECTION.
  " CREATE OBJECTS (INSTANCES)
  CREATE OBJECT:  INSTANCE1,
                  INSTANCE2,
                  INSTANCE3.
  " SET ATTRIBUTES OF INSTANCE1
  INSTANCE1->I_COUNT = INSTANCE1->I_COUNT + 10.
  INSTANCE1->NAME = 'VINO'.
  " SET ATTRIBUTES OF INSTANCE2
  INSTANCE2->I_COUNT = INSTANCE2->I_COUNT + 10.
  INSTANCE2->NAME = 'BALAJI'.
  " SET ATTRIBUTES OF INSTANCE3
  INSTANCE3->I_COUNT = INSTANCE3->I_COUNT + 10.
  INSTANCE3->NAME = 'HARI'.
  " DISPLAY ATTRIBUTES OF INSTANCE1
  WRITE: 'ATTRIBUTES OF INSTANCE 1 :::',
          / 'COUNT : ', INSTANCE1->I_COUNT,
          / 'NAME : ' , INSTANCE1->NAME.
  SKIP.
  SKIP.
  " DISPLAY ATTRIBUTES OF INSTANCE2
  WRITE: 'ATTRIBUTES OF INSTANCE 2 :::',
          / 'COUNT : ', INSTANCE2->I_COUNT,
          / 'NAME : ' , INSTANCE2->NAME.
  SKIP.
  SKIP.
  " DISPLAY ATTRIBUTES OF INSTANCE3
  WRITE: 'ATTRIBUTES OF INSTANCE 3 :::',
          / 'COUNT : ', INSTANCE3->I_COUNT,
          / 'NAME : ' , INSTANCE3->NAME.
  " CLEAR MEMORY OCCUPIED BY OBJECTS
  CLEAR: INSTANCE1,
         INSTANCE2,
         INSTANCE3.

Program Output : 001
ATTRIBUTES OF INSTANCE 1 :::
COUNT :          30
NAME :  VINO
ATTRIBUTES OF INSTANCE 2 :::
COUNT :          30
NAME :  BALAJI
ATTRIBUTES OF INSTANCE 3 :::
COUNT :          30
NAME :  HARI

No comments:

Post a Comment