Object Oriented SAP ABAP – Objects and Classes

FREE Online Courses: Your Passport to Excellence - Start Now

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?

  • Object Oriented programming breaks down tasks into objects that have data and function.
  • This method makes software programming understandable, flexible and reusable.
  • Object-Oriented Programing (OOP) was created with simplicity and eloquence in mind.
  • It makes people communicate clearly, exchange information and ideas using a common language – the language of objects.
  • These objects are made up of their two main characteristics – properties (data) and behaviour (or functions)

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 –

POPOOP
POP stands for Procedure-Oriented programmingOOP 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-solvingOOP follows bottom-up fashion in problem-solving
It divides a program into smaller parts called functions or modulesIt divides a program into smaller parts called objects
Functions share global data and data cannot be hidden from each otherFunctions do not share global data – you can decide who can access your data
There is not much emphasis on data protectionOne of the main points of focus in OOP is data protection
Extending functionality and addition of data is tedious and time-consumingIt is very easy to extend functionality and add new data, by simply adding new objects
Concepts of inheritance, polymorphism, encapsulation are not usedConcepts of inheritance, polymorphism, encapsulation form the very basis of OOP
E.g. C, Pascal, FORTRANE.g. Java, Python, C++

OOP in ABAP

  • Initially, SAP developed ABAP as a POP language.
  • However, as time progressed, they shifted it and transformed it into an OOP language
  • Hence, ABAP now follows the tenets of object orientation and paradigms like objects.
  • As part of OOP, ABAP uses the concepts of inheritance, overloading, data hiding, etc. for better functionality.

Let us begin by understanding a few basic terms of OOP

Basic terms to know

TERMMEANING
Objecta real-world entity that has data and behaviour
Classa complex collection of objects that mainly contain data fields and characteristics pertaining to the data
Methodbehaviour of an object i.e. function modules that the object performs
Membereither the data member or member function of a class
Instancesynonym of object, usually used as ‘instance of a class’
Instantiationcreating a new object of a class

Objects in SAP ABAP

  • An object is a real-world entity that contains data and behaviour.
  • These characteristics and behaviour define the state of the object, and the actions that the object will perform.
  • An object, as mentioned previously, is a blueprint or instance of a class.
  • For e.g. an employee, a book, an apple tree – all are objects.

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.

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

  • A class is a complex collection of objects
  • It contains data members (characteristics) and member methods (functions) together wrapped into a single entity called object
  • Classes are where objects are created and defined
  • We also define and declare function modules in a class
  • We can describe the privacy of a class
  • What this means is, we can define who all can have access to particular data of a class

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

  • Attributes are the characteristics of the class
  • They can have any pre-defined data type like P, F, I, etc.
  • We must declare attributes in class declaration section
  • Attributes are of two types:
    • Instance: this includes the instance-specific state of object, the changes are different for all objects, and this is declared by DATA statement
    • Static: this includes common state of the class to be shared by all instances of class, hence when we change one state then all other objects can see the change, declared by CLASS-DATA statement

Methods in SAP ABAP

  • A method is a module or a function
  • It is the behaviour of the object of a class
  • A method can access any characteristic of a class
  • It contains actions to be performed by objects that call upon the class
  • Method definition may or may not contain parameters, which are passed when the method is called
  • We can define a method using METHOD and ENDMETHOD statements

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

  • If we declare them public, they can be accessed by any class
  • When we declare them private, they cannot be accessed by any class
  • If we declare them protected, they can only be accessed by inheriting classes

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

  • Constructors are special types of methods
  • When we create an object, we need to initialise it and constructors do the job of initialising data for the objects
  • The program calls constructors automatically when an object of the class is created
  • The constructor will trigger the operation included whenever an object is newly created in execution

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

  • Whenever we redefine a variable within a method with a different value, that method will use the redefined value of the variable
  • However, if you want to access the originally declared value, then you can use the ME operator
  • Let us see an example of using the ME operator

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.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *