Site icon DataFlair

Object Oriented SAP ABAP – Objects and Classes

SAP ABAP OOPS

FREE Online Courses: Dive into Knowledge for Free. Learn More!

In this tutorial, we’ll be learning about one of the principles upon which ABAP is built – Object Oriented Programming.

We will learn about what OOP means, and also what it entails in the world of SAP ABAP.

What is OOP?

The main characteristics of OOP are –

  1. Data abstraction
  2. Encapsulation
  3. Emphasis on data rather than procedure
  4. Usage of real-world entities called ‘object’
  5. Data privacy and protection

POP vs OOP

Procedure-Oriented Programming (POP) approaches problems in a top-down method. It emphasizes procedure rather than data in a linear fashion.

Let us see a few differences between POP and OOP –

POP OOP
POP stands for Procedure-Oriented programming OOP stands for Object-Oriented programming
It focuses on procedure (doing tasks) rather than data (tasks) It focuses on data/tasks rather than procedure
POP follows top-down fashion in problem-solving OOP follows bottom-up fashion in problem-solving
It divides a program into smaller parts called functions or modules It divides a program into smaller parts called objects
Functions share global data and data cannot be hidden from each other Functions do not share global data – you can decide who can access your data
There is not much emphasis on data protection One of the main points of focus in OOP is data protection
Extending functionality and addition of data is tedious and time-consuming It is very easy to extend functionality and add new data, by simply adding new objects
Concepts of inheritance, polymorphism, encapsulation are not used Concepts of inheritance, polymorphism, encapsulation form the very basis of OOP
E.g. C, Pascal, FORTRAN E.g. Java, Python, C++

OOP in ABAP

Let us begin by understanding a few basic terms of OOP

Basic terms to know

TERM MEANING
Object a real-world entity that has data and behaviour
Class a complex collection of objects that mainly contain data fields and characteristics pertaining to the data
Method behaviour of an object i.e. function modules that the object performs
Member either the data member or member function of a class
Instance synonym of object, usually used as ‘instance of a class’
Instantiation creating a new object of a class

Objects in SAP ABAP

Creating an Object in ABAP

Let us see how to create an object in ABAP –

Syntax:

DATA: <object_name> TYPE REF TO <class_name>. “Create reference variable
  CREATE Object: <object_name>. #Create object from ref. var.

Example:

DATA: dataflairobject TYPE REF TO dataflairclass.
        CREATE Object: dataflairobject.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

Report example:

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

CLASS ClassDataFlair Definition.
Public Section.
DATA: dataflairtext(45) VALUE 'Data Flair OOPS tutorial in ABAP: data.'.
METHODS: DataFlairMethod.
ENDCLASS.

CLASS ClassDataFlair Implementation.
METHOD DataFlairMethod.
Write:/ 'Data Flair OOPS tutorial in ABAP: method.'.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
DATA: ClassDataFlair TYPE REF TO ClassDataFlair.
CREATE Object: ClassDataFlair.
Write:/ ClassDataFlair->dataflairtext.
CALL METHOD: ClassDataFlair->DataFlairMethod.

Output

Data Flair OOPS tutorial in ABAP: data.
Data Flair OOPS tutorial in ABAP: method.

Classes in SAP ABAP

Creating a Class in ABAP

Creating a class in ABAP has two steps –

1. Declaration

Syntax –

CLASS <class_name> DEFINITION.
//define class
ENDCLASS.

Example –

CLASS dataflairclass DEFINITION.
//define class
ENDCLASS.

2. Definition

Syntax –

CLASS <class_name> IMPLEMENTATION.
//define class
ENDCLASS.

Example –

CLASS dataflairclass IMPLEMENTATION.
//define class
ENDCLASS.

Attributes in SAP ABAP

Methods in SAP ABAP

Syntax:

METHOD

…

ENDMETHOD

Accessing Attributes and Methods in SAP ABAP

1. Methods and attributes can be accessed by objects of the class

2. The access specifiers – public, private and protected – work the same way as they do for class access specifiers

Let us see an example of how to write classes, attributes and methods and access them –

Example:

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

CLASS dataflairclass Definition.
   PUBLIC Section.
      Data: dataflairtext01 Type char25 Value 'Public Data Flair'.
      Methods dataflairmethod.

   PROTECTED Section.
      Data: dataflairtext02 Type char25 Value 'Protected Data Flair'.

   PRIVATE Section.
      Data: dataflairtext03 Type char25 Value 'Private Data Flair'.
ENDCLASS.

CLASS dataflairclass Implementation.
   Method dataflairmethod.
      Write: / 'Public Method:',
             / dataflairtext01,
             / dataflairtext02,
             / dataflairtext03.
      Skip.
   EndMethod.
ENDCLASS.

Start-Of-Selection.
   Data: Objectx Type Ref To dataflairclass.
   Create Object: Objectx.
   CALL Method: Objectx->dataflairmethod.
   Write: / Objectx->dataflairtext01.

Output

Public Method:
Public Data Flair
Protected Data Flair
Private Data FlairPublic Data Flair

Example of Static Attributes in SAP ABAP

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

CLASS dataflairclass Definition.
   PUBLIC Section.
      CLASS-DATA: dataflairname Type char45,
                  dataflairdata Type I.
   Methods: dataflairmethod.
ENDCLASS.

CLASS dataflairclass Implementation.
   Method dataflairmethod.
      Do 4 Times.
         dataflairdata = 1 + dataflairdata.
         Write: / dataflairdata, dataflairname.
      EndDo.
      Skip.
   EndMethod.
ENDCLASS.

Start-Of-Selection.
   dataflairclass=>dataflairname = 'ABAP Object Oriented Programming'.
   dataflairclass=>dataflairdata = 0.
   Data: Object1 Type Ref To dataflairclass,
         Object2 Type Ref To dataflairclass.

   Create Object: Object1, Object2.
   CALL Method: Object1->dataflairmethod,
                Object2->dataflairmethod.

Constructors in SAP ABAP

Example of ABAP Constructors

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

CLASS dataflairclass Definition.
   PUBLIC Section.
      Methods: dataflairmethod, constructor.
ENDCLASS.

CLASS dataflairclass Implementation.
   Method dataflairmethod.
      Write: / 'This is the method'.
   EndMethod.

   Method constructor.
      Write: / 'This is the constructor'.
   EndMethod.
ENDCLASS.

Start-Of-Selection.
   Data Object1 Type Ref To dataflairclass.
   Create Object Object1.

Output

This is the constructor

ME Operator in SAP ABAP

Example of ME Operator in ABAP

REPORT ZR_SS_DATAFLAIR_SAMPLE_001.

CLASS dataflairclass Definition.
   PUBLIC Section.

Data dataflairtext Type char25 Value 'Here we have class value'.
   Methods dataflairmethod.
ENDCLASS.

CLASS dataflairclass Implementation.
   Method dataflairmethod.

Data dataflairtext Type char25 Value 'Here we have method value'.
   Write: / ME->dataflairtext,
          / dataflairtext.
   ENDMethod.
ENDCLASS.

Start-Of-Selection.
   Data objectx Type Ref To dataflairclass.
   Create Object objectx.
   CALL Method objectx->dataflairmethod.

Output

Here we have class value
Here we have method value

Summary

Thus, in this tutorial, we learnt about the basics upon which ABAP and many other languages are built – object oriented programming. We learnt about how object orientation is different from procedure orientation, its importance and most important terms.

In part #2 of this tutorial, we will dive into further concepts of object orientation like inheritance, polymorphism, etc.

Exit mobile version