ABAP Objects for Beginners. Part 1 – Introduction

There have been numerous requests from freshers to start a series on Step by Step Tutorials on Object-Oriented Programming in SAP ABAP. For quite sometimes, we procrastinated the idea because we believed, there are numerous portals which have good ABAP Object articles.

But, our valued readers like the way we present and its simplicity and they insisted, we have our own series. So, here we go friends. This is the first part and if you are an experienced ABAPer, you do not need to read this. But for Beginners, this is a good starting point for your ABAP Objects learning experience.

Commonly Used Terms in ABAP Object Developments

A) Class: A class is a user-defined data type with Attributes, Methods, Events, and interfaces for a business application.

Types of Classes:

  1. Local Class
  2. Global Class

Local class: Local classes are defined in an ABAP program (Transaction SE38) and can only be used in the program in which they are defined.

Global Class: Global classes and interfaces are defined in the Class Builder (Transaction SE24) in the ABAP Workbench.

All of the ABAP programs in the SAP System can access the global classes. Hence the name global. We will discuss Global classes in coming articles.

Every class will have two Sections.

  1. Definition
  2. Implementation

Definition: This section is used for declaration of the components of classes such as attributes, methods events.

They are enclosed in the ABAP statements CLASS DEFINITION ……. END CLASS.

CLASS <class name> DEFINITION.
…..
END CLASS.

Implementation: This section of a class contains the implementation of all methods of the class. The Implementation part of the local class is the processing block.

They are enclosed in the ABAP statements CLASS IMPLEMENTATION ……. END CLASS.

CLASS <class name> IMPLEMENTATION.
………
END CLASS.

B) Structure of a class:

The following statements define the structure of the class.

  • A class contains Components.
  • Each component is assigned to a Visibility section
  • Classes Implement methods.

Components of aClass:

  1. Attributes
    • Static Attributes
    • Instance Attributes
  2. Methods
    • Static Methods
    • Instance Methods
  3. Events
  4. Interfaces
  • Static Components: (Attributes / Methods)

These Static components exist globally for a class and are referred to using static component selector =>. In the code, if you see => or use => then you should correlate that it is Static Component (it can be an attribute i.e. variables/constants or it might be methods).

The static component only exists once per class and are valid for all instances of the class. They are declared with the CLASS- keywords

  • Instance Components: (Attributes / Methods)

These Instance components exist separately in each instance (object) of the class and are referred using instance component selector using ->. So know you know the difference between => and -> in ABAP Objects.

We will dedicate a whole article on Static and Instant concept in future posts.

  1. Attributes: Any data, constants, types declared within a class form the attribute of the class.
  2. Methods: Block of code providing some functionality offered by the class can be compared to function modules or subroutines/performs. They can access all of the attributes of a class.

Methods are defined in the definition part of class and implemented in the implementation part using the processing block.

Declaration:

METHODS <Method name>.

Implementation:

METHOD <method name>
………
END METHOD.

Methods are called using the CALL METHOD statement.

3. EVENTS: A mechanism set within the class which can help a class to trigger methods of another class.

4. Interfaces: Interfaces are independent structures that you can implement in a class to extend the scope of the class.

C) Visibility Components of the Class:

Each Component of a class must have a Visibility. In ABAP Classes, the whole class definition is divided into three visibility sections.

  1. Public Section
  2. Protected Section
  3. Private Section
CLASS <class_name> DEFINITION.
 
PUBLIC SECTION.
……..
PROTECTED SECTION.
……………
PRIVATE SECTION.
………..
 
END CLASS.

Public section: Data declared in public section can be accessed by the class itself, by its subclasses as well as by other users outside the class.

Protected section: Data declared in the protected section can be accessed by the class itself, and also by its subclasses but not by external users outside the class.

Private Section: Data declared in the private section can be accessed by the class only, but not by its subclasses and by external users outside the class.

Please note: They (Public/Protected/Private) have to be defined in the order shown above.

D) 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.

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>.

Small Example of Class:

<em>*&---------------------------------------------------------------------*</em>
 <em>*& Report  YRAM_OOABAP1</em>
 <em>*======================================================================*</em>
 <em>* Project : SAP Object Oriented ABAP *</em>
 <em>* Description : Displaying the name by using OO ABAP *</em>
 <em>*======================================================================*</em>
REPORT yram_ooabap1.
 
CLASS lcl_class DEFINITION.
  PUBLIC SECTION.
**~~** Declaration of Instance Variable
    DATA : lv_name TYPE char20 VALUE 'www.sapspot.com'.
**~~** Declaration of Instance Method
    METHODS : show_name.
  PROTECTED SECTION.
    "No Declaratons
  PRIVATE SECTION.
    "No Declaratons
ENDCLASS.
 
*&---------------------------------------------------------------------*
**&&~~ Class Implementation
*&---------------------------------------------------------------------*
CLASS lcl_class IMPLEMENTATION.
 
**~~** Implementation of method
  METHOD show_name.
    WRITE :/ 'This is the SHOW_NAME method'.
    WRITE :/5 lv_name.  " Public Varible
  ENDMETHOD.
 
ENDCLASS.
 
*======================================================================*
* START of TREATMENT *
*======================================================================*
START-OF-SELECTION.
**&&~~ Declaration of Object
  DATA : obj1 TYPE REF TO lcl_class.
**&&~~ Creation of the Object
  CREATE OBJECT obj1.
**&&~~ Calling the method
  CALL METHOD obj1->show_name.

OUTPUT:

Rating: 0 / 5 (0 votes)