Objects in ABAP

Introduction

Objects in ABAP are special variables that have distinct characteristics and behaviors in general terms known as attributes and methods. The attributes define the state of an object and a method defines the behavior or actions performed by the object.

An object has three main characteristics-

  • State – State defines the set of attributes and its values.
  • Identity – Each object has a unique identity.
  • Behaviour – Behaviour defines the actions performed.

The two objects may have the same state or behaviour but their identity is always unique. The identity of object sustains till its lifetime.

For example, a student has attributes like student name (first name, last name), student id, date of birth, address and behaviour would be the change in its attributes. The two students may have the same attributes and behaviour but they are not identical. Their identity is unique and will never change throughout their lifetime.

Objects interact with each other by sending messages.

Definition

An object is the instance of the class. It is a special variable that has distinct characteristics and behaviours.

ABAP Objects creation

Object creation includes two steps. They are as follows:

  • Creating reference variable to class.

DATA : <object_name> TYPE REF TO <class_name>.

  • Creating an object from the reference variable.

CREATE OBJECT : <object_name>.

Example

Let’s jot down an example.

****class definition****

CLASS zdemo DEFINITION.

                PUBLIC SECTION.

                                 METHOD : zdemo_method.

END CLASS.

****class implementation****

CLASS zdemo IMPLEMENTATION.

                METHOD zdemo_method.

                                WRITE : “ABAP Object”

                END METHOD.

END CLASS.

****creating class object****

START-OF-SELECTION.

DATA : object_demo TYPE REF TO zdemo.                                             “Creating reference variable”

CREATE OBJECT object_demo.                                                                   “Creating object”

CALL METHOD : object_demo->zdemo.

Objects in ABAP

In the example, zdemo_method is the method defined in the public section of definition part of the class zdemo. The implementation part of the method is provided in the class implementation. Firstly, we have created the reference variable object_demo to the class and then created an object object_demo from the reference variable. The object_demo->zdemo calls of the method zdemo_method and executes the code inside the method.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.