ABAP OO - Basics
In this article we take a look at the basics of development in the object-oriented area and start a small series of articles.
If you should be looking for a new job again, you will notice that it almost always says: Developer with ABAP & ABAP OO. Actually, this should be a basic understanding for every developer that he has mastered the latest technologies and language elements. Unfortunately, it looks different for many developers. You have heard of ABAP OO, but you would use it little or only in a simple style. Solid and strong OO knowledge is required for many clean architectures, including within SAP. At this point we want to start with our little basic course.
In contrast to functional programming, the object-oriented world primarily relies on the use of encapsulated objects in order to create a closed model. This model has attributes and methods that a user can ultimately use to interact with the object. The class serves as a kind of blueprint that can be used over and over again. By separating the functions and attributes, the class can be used again and again, you can already find this behavior today in function groups and with function modules, although you have to make some compromises, as the data is not automatically deleted in the function group (global data) .
This means that classes and methods can be reused in the entire system, which increases the reuse of such objects and is one of the strengths of classes.
In this section we want to give you a few examples of what such reusable classes can look like and where they can be used everywhere.
- Logging - There is a very central framework in SAP for logging messages, the BALLOG. In order to interact with the framework, there are various modules and configurations that can accept, display or save the messages in the database. The functions, the storage of messages, the display and the backup on the database can easily be implemented in a reusable class so that you don't have to take over all function blocks every time or maybe even copy logics from one program to the next.
- Data mapping - Do you map BAPI to database data and do that in both directions? You will probably also be able to use this mapping in a next program. Therefore, you should implement it in a global class so that you can reuse the functions at any time.
- Processing - Do you carry out various processing steps in a chain and each step is quite complex? Here you shouldn't pack everything in a report or build a monster class, but implement functions such as data validation, mapping and booking in various reusable objects.
As you can see, many of the examples are about reusing code in other places. The worst approach would be to copy such code, since you would also copy errors that you can later correct in two or three places.
Hint: With every development, DRY (Don't repeat yourself) should therefore come first, if you notice you are repeating yourself, stop and start refactoring.
In contrast to Java or other object-oriented languages, there are also some special features in the ABAP environment that should be taken into account.
- Constructor - ABAP classes have two constructors, the CONSTRUCTOR which is called with every instantiation and the CLASS_CONSTRUCTOR which is executed when the class is used for the first time, regardless of whether you are using constants, static methods or an instance.
- Destructor - There is no real destructor that can be created. Before you want to delete an object, you had to implement a method yourself (e.g. FREE or CLEAN) and call it.
- Single inheritance - Each class can have at most one superclass from which it is inherited (attributes, methods).
- Interfaces - Can be nested with one another to form larger interfaces and have public attributes, which are then inherited accordingly in the implementing classes.
In the object-oriented environment you will come across the same terms over and over again, which is why we want to bring you closer to the most important terms and explain the background.
Is the actual blueprint for an object and contains the attributes and methods that the user can use in the end. The simplest implementation of such a definition can look like this:
CLASS lcl_example DEFINITION. ENDCLASS.
The two terms are used in the same way and describe a "variable" that instantiates a usable class. The instantiation is done by CREATE OBJECT or better by NEW:
DATA(go_instance) = NEW lcl_example( ).
As you saw above, an object is created by instantiating the class (the blueprint) and creating a reference to a variable. A class has become an object or an instance.
An attribute is a variable or constant in a class that you can use and hold data in. An attribute behaves like a global variable in a class and is available to every method.
CLASS lcl_example DEFINITION. PUBLIC SECTION. DATA: gd_flag TYPE abap_bool. ENDCLASS.
The method is like FORM or a function module in a class, it carries out various actions within the class, changes variables or simply returns a result.
CLASS lcl_example DEFINITION. PUBLIC SECTION. METHOD: calculate_correct_value IMPORTING id_value TYPE string RETURNING VALUE(rd_result) TYPE i. ENDCLASS.
Before we start with the actual examples and the coding, we wanted to give you a brief introduction to the subject of object orientation in ABAP. In the next article, we'll go a little deeper into the basics of programming with objects.