SAP ABAP - Objects



An object is a special kind of variable that has distinct characteristics and behaviors. The characteristics or attributes of an object are used to describe the state of an object, and behaviors or methods represent the actions performed by an object.

An object is a pattern or instance of a class. It represents a real-world entity such as a person or a programming entity like variables and constants. For example, accounts and students are examples of real-world entities. But hardware and software components of a computer are examples of programming entities.

An object has the following three main characteristics −

  • Has a state.
  • Has a unique identity.
  • May or may not display the behavior.

The state of an object can be described as a set of attributes and their values. For example, a bank account has a set of attributes such as Account Number, Name, Account Type, Balance, and values of all these attributes. The behavior of an object refers to the changes that occur in its attributes over a period of time.

Each object has a unique identity that can be used to distinguish it from other objects. Two objects may exhibit the same behavior and they may or may not have the same state, but they never have the same identity. Two persons may have the same name, age, and gender but they are not identical. Similarly, the identity of an object will never change throughout its lifetime.

Objects can interact with one another by sending messages. Objects contain data and code to manipulate the data. An object can also be used as a user-defined data type with the help of a class. Objects are also called variables of the type class. After defining a class, you can create any number of objects belonging to that class. Each object is associated with the data of the type class with which it has been created.

Creating an Object

The object creation usually includes the following steps −

  • Creating a reference variable with reference to the class. The syntax for which is −

DATA: <object_name> TYPE REF TO <class_name>.
  • Creating an object from the reference variable. The syntax for which is −

CREATE Object: <object_name>.

Example

REPORT ZDEMO_OBJECT. 
CLASS Class1 Definition. 
Public Section. 
DATA: text1(45) VALUE 'ABAP Objects.'. 
METHODS: Display1. 
ENDCLASS. 

CLASS Class1 Implementation. 
METHOD Display1. 
Write:/ 'This is the Display method.'. 
ENDMETHOD. 
ENDCLASS. 

START-OF-SELECTION. 
DATA: Class1 TYPE REF TO Class1. 
CREATE Object: Class1. 
Write:/ Class1->text1. 
CALL METHOD: Class1->Display1.

The above code produces the following output −

ABAP Objects. 
This is the Display method.
Advertisements