Attributes in ABAP Object-Oriented Programming | ABAP Insights



 Understanding Attributes in ABAP Object-Oriented Programming

In Object-Oriented Programming (OOP), an attribute refers to a property or characteristic of an object or class. In ABAP, attributes hold data values and describe the state of an object or class.

 

For example, if you have a class to represent a car, the attributes could be things like color, model, speed, etc.

---

 Types of Attributes in ABAP OOP

There are two main types of attributes in ABAP:

1. Instance Attributes

2. Static Attributes

 

 1. Instance Attributes

Instance attributes are specific to each object of a class. Every object (or instance) has its own set of instance attributes. Changes made to instance attributes of one object do not affect other objects.

 

- Example: 

   If you create two different car objects, one car can have a color attribute set to red, while another car can have the color set to blue.

- Usage in ABAP:

   abap

   CLASS lcl_car DEFINITION.

     PUBLIC SECTION.

       DATA: color TYPE string.  " Instance Attribute

   ENDCLASS.

   CLASS lcl_car IMPLEMENTATION.

   ENDCLASS.

 

   DATA: car1 TYPE REF TO lcl_car,

         car2 TYPE REF TO lcl_car.

 

   CREATE OBJECT car1.

   car1->color = 'Red'.

 

   CREATE OBJECT car2.

   car2->color = 'Blue'.

 

   WRITE: / 'Car 1 color:', car1->color.

   WRITE: / 'Car 2 color:', car2->color.

  

   Output:

   Car 1 color: Red

   Car 2 color: Blue

Each object (car1, car2) has its own color value.

 

 2. Static Attributes

Static attributes belong to the class itself, not to any particular object. All objects of the class share the same static attribute, meaning if one object changes the static attribute, it affects all other objects of the class. 

- Example: 

   If you create two different car objects, but there is a static attribute like total_number_of_cars, all objects will share the same value.

- Usage in ABAP:

   abap

   CLASS lcl_car DEFINITION.

     PUBLIC SECTION.

       CLASS-DATA: total_number_of_cars TYPE i.  " Static Attribute

   ENDCLASS.

   CLASS lcl_car IMPLEMENTATION.

   ENDCLASS.

 

   DATA: car1 TYPE REF TO lcl_car,

         car2 TYPE REF TO lcl_car.

 

   CREATE OBJECT car1.

   lcl_car=>total_number_of_cars = 1.  " Static attribute

 

   CREATE OBJECT car2.

   lcl_car=>total_number_of_cars = lcl_car=>total_number_of_cars + 1.

 

   WRITE: / 'Total number of cars:', lcl_car=>total_number_of_cars.

  

   Output:

   Total number of cars: 2

 

Both objects share the same total_number_of_cars attribute, and the change affects all objects.

---

 Key Differences Between Instance and Static Attributes

 

Aspect               

Instance Attribute                       

Static Attribute                           

Ownership             

Belongs to each individual object            

Belongs to the class itself                    

Scope

Specific to an object                        

Shared by all instances of the class           

Lifetime

Exists as long as the object exists          

Exists for the lifetime of the class           

Change Effect         

Changes affect only that object              

Changes affect all objects of the class 

---

 Using Attributes in an ABAP OOP Program

Here is an example where both instance and static attributes are used:

abap

CLASS lcl_car DEFINITION.

  PUBLIC SECTION.

    DATA: color TYPE string.               " Instance Attribute

    CLASS-DATA: total_number_of_cars TYPE i.  " Static Attribute

    METHODS: display_details.

ENDCLASS.

 

CLASS lcl_car IMPLEMENTATION.

  METHOD display_details.

    WRITE: / 'Car color:', color, 'Total cars:', total_number_of_cars.

  ENDMETHOD.

ENDCLASS.

 

DATA: car1 TYPE REF TO lcl_car,

      car2 TYPE REF TO lcl_car.

 

CREATE OBJECT car1.

car1->color = 'Red'.

lcl_car=>total_number_of_cars = lcl_car=>total_number_of_cars + 1.

 

CREATE OBJECT car2.

car2->color = 'Blue'.

lcl_car=>total_number_of_cars = lcl_car=>total_number_of_cars + 1.

 

car1->display_details().

car2->display_details().

 

 

Output:

 

Car color: Red   Total cars: 2

Car color: Blue  Total cars: 2


In this example:

- Each car object has its own color (instance attribute).

- Both objects share the same static attribute total_number_of_cars.

---

 Conclusion

Attributes play a crucial role in describing the properties of an object in ABAP OOP. Instance attributes are unique to each object, while static attributes are shared among all instances of the class. By understanding these concepts, you can design more efficient and organized object-oriented programs.

Post a Comment

Previous Post Next Post