Object-Oriented Programming in SAP ABAP: Concepts and Pillars
OOPS concepts every ABAP developer should know

ABAP Developer | SAP Enthusiast | Writing about modern ABAP, clean code, different concepts in ABAP, real project learnings and challenges faced.
Object Oriented Programming (OOPS) plays a crucial role in modern SAP ABAP development, especially with the introduction of ABAP objects. Unlike procedural programming, OOPS helps developers write cleaner, modular and reusable code, making applications easier to maintain and extend.
In this blog, I will explain the core OOPS concepts along with the four main pillars - Encapsulation, Abstraction, Inheritance, and Polymorphism along with simple ABAP examples.
1) Basic OOPS Concepts in SAP ABAP:
a) Class:
A class defines the data (attributes) and behavior(methods) of objects. It is a blueprint used to create objects. A class itself does not hold data; instead it describes how the object should behave.
b) Object:
An object is a runtime instance of a class. While a class defines the structure and behavior, an object represents the actual entity that uses that structure in the program.
c) Attributes:
Attributes are variables and constants declared within a class. They represent the state or properties of an object.
d) Methods:
Methods are the coding blocks which provide functionality like function modules. They are declared inside a class and can be called using an object reference.
"Class Definition
CLASS zcl_employee DEFINITION.
PUBLIC SECTION.
DATA: gv_name TYPE char50, " Attributes
gv_id TYPE i.
METHODS: display_details.
ENDCLASS.
"Class Implementation
CLASS zcl_employee IMPLEMENTATION.
METHOD display_details. " Method
WRITE: / 'Employee ID:', gv_id,
/ 'Employee Name:', gv_name.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
"Creating an Object of a class
DATA(lo_employee) = NEW zcl_employee( ).
"Assigning the values to the attributes
lo_employee->gv_id = '101'.
lo_employee->gv_name = 'Dipesh'.
"Calling the method
lo_employee->display_details( ).
Output:
Here from the above example:
Class: zcl_employee defines the structure and behavior.
Attributes: gv_id and gv_name store the data.
Methods: display_details shows the data.
Object: lo_employee is an instance of the class.
2) Core OOPS Elements in SAP ABAP:
a) Types of Classes in ABAP OOP:
Global Class: Classes created in SE24 transaction are known as Global Class. The class created in SE24 can be accessed by all ABAP programs across the system.
Step 1: Create a global class through TCode SE24
Step 2: Create a method
Step 3: Add the parameters to the Method
Step 4: Write a sample code inside the Method
Step 5: Call the method through the program SE38
"Declarations
DATA: lt_vbap TYPE STANDARD TABLE OF vbap,
lo_sales TYPE REF TO ydvk_global_class.
"Creating the object
CREATE OBJECT lo_sales.
"Calling the method
CALL METHOD lo_sales->fetch_sales_details
EXPORTING
iv_vbeln = '1234567890'
IMPORTING
it_vbap = lt_vbap.
Local Class: Class created in SE38 as an ABAP program or in include is known as Local class. These are limited to the program in which it is defined or created.
"Definition of the class
CLASS zcl_department DEFINITION.
PUBLIC SECTION.
DATA: gv_name TYPE char50,
gv_id TYPE i.
METHODS: display_details.
ENDCLASS.
"Implementation of the class
CLASS zcl_department IMPLEMENTATION.
METHOD display_details.
WRITE: / 'Dept ID:', gv_id,
/ 'Department Name:', gv_name.
ENDMETHOD.
ENDCLASS.
b) Visibility of a class in ABAP OOP:
It refers to how its attributes, methods and events can be accessed. Visibility is controlled by sections within the class definition: PUBLIC, PROTECTED, and PRIVATE.
PUBLIC SECTION: can be accessed by that class and other class and subclass of the program.
PROTECTED SECTION: can be accessed by that class and sub class or derived class only. PRIVATE SECTION: can be accessed by that class and not by any other class or subclass.
"Definition
CLASS zclass_demo DEFINITION.
PUBLIC SECTION. " Accessed outside as well inside the class
DATA: gv_vbeln_pub TYPE vbeln.
METHODS: hello_world_pub,
call_private. " Public method to call private method
PROTECTED SECTION. " Limited upto to this class and subclasses
DATA: gv_vbeln_pro TYPE vbeln.
METHODS: hello_world_pro.
PRIVATE SECTION. " Limited upto this class endclass
DATA: gv_vbeln_pri TYPE vbeln.
METHODS: hello_world_pri.
ENDCLASS.
"Implementation
CLASS zclass_demo IMPLEMENTATION.
METHOD hello_world_pri.
WRITE 'Hello World Private Section'.
ENDMETHOD.
METHOD hello_world_pro.
WRITE 'Hello World Protected Section'.
ENDMETHOD.
METHOD hello_world_pub.
WRITE 'Hello World Public Section'.
IF gv_vbeln_pub IS NOT INITIAL.
WRITE: / gv_vbeln_pub.
ENDIF.
ENDMETHOD.
METHOD call_private.
CALL METHOD hello_world_pri( ).
gv_vbeln_pri = '123456789'.
WRITE: / gv_vbeln_pri.
ENDMETHOD.
ENDCLASS.
"Definition of a subclass
CLASS zclass_sub DEFINITION INHERITING FROM zclass_demo.
PUBLIC SECTION.
METHODS call_protected.
ENDCLASS.
"Implementation of a subclass
CLASS zclass_sub IMPLEMENTATION.
METHOD call_protected.
hello_world_pro( ). " Accessing protected method from subclass
gv_vbeln_pro = '12345678'. " Accessing protected attribute
ENDMETHOD.
ENDCLASS.
PUBLIC Section Visibility Example:
hello_world_pub( ) can be called directly from outside the class.
gv_vbeln_pub can be set or read from outside.
START-OF-SELECTION.
DATA(lo_class) = NEW zclass_demo( ).
lo_class->gv_vbeln_pub = '12345678'.
lo_class->hello_world_pub( ).
Output:
PROTECTED Section Visibility Example:
hello_world_pro and gv_vbeln_pro are not accessible directly from the outside.
They can be accessed inside a subclass(zclass_sub).
START-OF-SELECTION.
DATA(lo_sub) = NEW zclass_sub( ).
lo_sub->call_protected( ).
Output:
PRIVATE Section Visibility Example:
hello_world_pri and gv_vbeln_pri are strictly internal. They cannot be used directly.
They can only be used inside zclass_demo itself (Eg: called from another method of the same class).
START-OF-SELECTION.
DATA(lo_private) = NEW zclass_demo( ).
"Call public method which internally used private method
lo_private->call_private( ).
Output:
c) Types of Methods in ABAP OOP:
Instance Methods: Instance methods belong to an instance of a class. They are declared using statement METHODS. They are called using object reference.
CLASS zclass_methods DEFINITION.
PUBLIC SECTION.
METHODS: hello_world_inst_meth.
ENDCLASS.
CLASS zclass_methods IMPLEMENTATION.
METHOD hello_world_inst_meth.
WRITE: / 'Instance Method is called'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
" An Instance of a class is created
DATA(lo_instance) = NEW zclass_methods( ).
lo_instance->hello_world_inst_meth( ).
Output:
Static Methods: Static methods belong to the class itself. They are declared using statement CLASS-METHODS. They can be called using the class name without creating an instance of class.
CLASS zclass_methods DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: hello_world_sta_pub.
ENDCLASS.
CLASS zclass_methods IMPLEMENTATION.
METHOD hello_world_sta_pub.
WRITE: / 'Static Method is called'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
" Object is not needed to call a static method
zclass_methods=>hello_world_sta_pub( ).
Output:
Constructor: Constructor is a method that will be called by default at the time of instantiation. It cannot be called using call method. Create a normal method and create an object of a class and the method constructor will get called by default.
CLASS zclass_constructor DEFINITION .
PUBLIC SECTION.
METHODS: normal_method,
constructor . " Instance constructor
CLASS-METHODS: class_constructor. " Class constructor(Static Constructor)
ENDCLASS.
CLASS zclass_constructor IMPLEMENTATION.
METHOD normal_method.
WRITE: / 'This is a normal method'.
ENDMETHOD.
METHOD constructor.
WRITE: / 'This is an instance constructor method.'.
ENDMETHOD.
METHOD class_constructor.
WRITE: / 'This is a class constructor method'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_constructor) = NEW zclass_constructor( ). " Constructor runs automatically
lo_constructor->normal_method( ).
Output:
d) Types of Objects in ABAP OOP:
Instance Objects: An instance object is created from a class during runtime using the NEW keyword or CREATE OBJECT statement. Each instance has its own copy of attributes.
CLASS zcl_employee DEFINITION.
PUBLIC SECTION.
METHODS: display_details_instance.
ENDCLASS.
CLASS zcl_employee IMPLEMENTATION.
METHOD display_details_instance.
WRITE: / 'Instance Object Example is used'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
"There are two ways to use an Instance Object
"1st way
DATA: lo_static_object TYPE REF TO zcl_employee.
CREATE OBJECT lo_static_object.
lo_static_object->display_details_instance( ).
"2nd way
DATA(lo_stat_obj) = NEW zcl_employee( ).
lo_stat_obj->display_details_instance( ).
Output:
Static Objects: Static context in ABAP refers to CLASS-METHODS and CLASS-DATA. They belong to the class itself, not to any instance. They are accessed directly via the class name.
CLASS zcl_employee DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: display_details_static.
ENDCLASS.
CLASS zcl_employee IMPLEMENTATION.
METHOD display_details_static.
WRITE: / 'Static Object Example is used'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
"No object required
zcl_employee=>display_details_static( ).
"Instance Object can be used to call a Static Method
DATA(lo_stat_obj) = NEW zcl_employee( ).
lo_stat_obj->display_details_static( ).
Output:
e) Types of Attributes in ABAP OOP:
Instance Attributes: Instance attributes belong to a specific object. They are declared using DATA keyword. Each object has its own copy of the attribute.
CLASS zcl_customer DEFINITION.
PUBLIC SECTION.
DATA: gv_name TYPE char50.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_emp1) = NEW zcl_customer( ).
DATA(lo_emp2) = NEW zcl_customer( ).
"Both the objects are different from each other. Each object stores its own value.
lo_emp1->gv_name = 'Dipesh'.
lo_emp2->gv_name = 'Varun'.
Static Attributes: Static Attributes belong to the class itself rather than a specific object. They are declared using the CLASS-DATA keyword. Only one shared copy exists.
CLASS zcl_customer DEFINITION.
PUBLIC SECTION.
CLASS-DATA: gv_name TYPE char50.
ENDCLASS.
START-OF-SELECTION.
"All the objects of the class will have the same GV_NAME
zcl_customer=>gv_name = 'Dipesh'.
3) The Four Pillars of OOPS:
a) Encapsulation: It is the concept of binding data (attributes) and methods (behavior) together inside a class and restricting direct access to the internal data. In simple words, it protects internal details by exposing only what's necessary through public methods.
Advantages:
Protects data from unauthorized access.
Ensures data integrity & maintainability.
b) Abstraction: It is the concept of hiding implementation details and exposing only the necessary functionality. In simple words, show what is necessary, hide the rest.
Advantages:
Reduce complexity & dependencies.
Make code easier to maintain and improve system design.
c) Inheritance: It is the ability of one class (child class) to acquire the properties and methods of another class (parent class). In simple words, a child class can reuse the code of a parent class.
Advantages:
Code reusability & reduce redundancy.
Enables hierarchical class structures.
d) Polymorphism: It is ability of different classes to respond to the same method call in different ways. In simple words, one interface, multiple implementations.
Advantages:
Improves flexibility.
Makes systems more extensible.
CLASS lcl_vehicle DEFINITION ABSTRACT. " Abstraction
PUBLIC SECTION.
METHODS: start_engine ABSTRACT.
ENDCLASS.
CLASS lcl_car DEFINITION INHERITING FROM lcl_vehicle. " Inheritance
PUBLIC SECTION.
METHODS: start_engine REDEFINITION.
PRIVATE SECTION.
* Encapsulation - GV_CAR can't be directly changed,
* it must go through methods
DATA: gv_car TYPE char50.
ENDCLASS.
CLASS lcl_car IMPLEMENTATION.
METHOD start_engine.
WRITE: / 'Car engine started'.
gv_car = 'Encapsulation Car engine'.
WRITE: / gv_car.
ENDMETHOD.
ENDCLASS.
CLASS lcl_bike DEFINITION INHERITING FROM lcl_vehicle. " Inheritance
PUBLIC SECTION.
METHODS: start_engine REDEFINITION.
ENDCLASS.
CLASS lcl_bike IMPLEMENTATION.
METHOD start_engine.
WRITE: / 'Bike engine started'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
*** Abstraction - just definition, implementation done in sub classes.
DATA: lo_pillars TYPE REF TO lcl_vehicle.
*** Polymorphism
lo_pillars = NEW lcl_car( ).
lo_pillars->start_engine( ).
lo_pillars = NEW lcl_bike( ).
lo_pillars->start_engine( ).
Output:
4) Advanced OOPS Concepts in SAP ABAP:
a) Interface:
An interface is a blueprint that defines a set of methods, attributes and constants that a class must implement. It does not contain any implementation, only declaration. All the methods in an interface are abstract by default, and implementation must be provided for all its declared methods. It is defined using INTERFACE, and implemented using INTERFACES keyword. In simple words, An interface defines "what should be done", but not "how it should be done".
" Interface Definition
INTERFACE lif_interface.
METHODS: display_name,
display_profile.
ENDINTERFACE.
" Implement Interface in a Class
CLASS lcl_int_class DEFINITION.
PUBLIC SECTION.
INTERFACES: lif_interface.
ENDCLASS.
CLASS lcl_int_class IMPLEMENTATION.
METHOD lif_interface~display_name.
WRITE: / 'My name is Dipesh.'.
ENDMETHOD.
METHOD lif_interface~display_profile.
WRITE: / 'I am an ABAP Developer.'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_interface) = NEW lcl_int_class( ).
lo_interface->lif_interface~display_name( ).
lo_interface->lif_interface~display_profile( ).
Output:
b) Friend Class:
A Friend Class is a special type of class that is allowed to access the private and protected components of another class. Normally private attributes and methods of a class cannot be accessed from outside the class. However, when a class is declared as a friend, it gets special permission to access those restricted components. To declare, FRIENDS keyword is used in the class definition. In simple words, A friend class is a trusted class that can access the private data of another class.
CLASS zcl_student1 DEFINITION.
PUBLIC SECTION.
DATA: gv_percentage TYPE p DECIMALS 2.
METHODS: show_percentage.
ENDCLASS.
CLASS zcl_student2 DEFINITION FRIENDS zcl_student1.
PRIVATE SECTION.
DATA: gv_percentage TYPE p DECIMALS 2 VALUE '90.40'.
ENDCLASS.
CLASS zcl_student1 IMPLEMENTATION.
METHOD show_percentage.
"Accessing Private attributes of zcl_student2
DATA(lo_student2) = NEW zcl_student2( ).
gv_percentage = lo_student2->gv_percentage.
WRITE: / 'Student2 percentage is', gv_percentage.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_student1) = NEW zcl_student1( ).
lo_student1->show_percentage( ).
Output:
c) Method Parameters:
Method parameters are used in receiving data as an input, modify or validate data inside the method and export or return the output in where it is called. The four main types of parameters are as follows:
IMPORTING: Used to pass values to the method. Input to the method.
EXPORTING: Used to pass values out of a method back to the caller. Output of the method.
CHANGING: Used when a variable needs to be both passed and modified inside it.
RETURNING: Used to return a single value from the method.
CLASS zcl_math DEFINITION.
PUBLIC SECTION.
METHODS add_numbers
IMPORTING iv_num1 TYPE i
iv_num2 TYPE i
EXPORTING ev_sum TYPE i.
METHODS square_number
CHANGING cv_num TYPE i.
METHODS get_pi
RETURNING VALUE(rv_pi) TYPE char4.
ENDCLASS.
CLASS zcl_math IMPLEMENTATION.
METHOD add_numbers.
ev_sum = iv_num1 + iv_num2.
ENDMETHOD.
METHOD square_number.
cv_num = cv_num * cv_num.
ENDMETHOD.
METHOD get_pi.
rv_pi = '3.14'.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: lv_sum TYPE i,
lv_num TYPE i,
lv_pi TYPE char4.
DATA(lo_math) = NEW zcl_math( ).
" Importing + Exporting
" While using in method call, EXPORTING & IMPORTING are flipped
" Similar to Function modules
lo_math->add_numbers( EXPORTING iv_num1 = 10 iv_num2 = 20
IMPORTING ev_sum = lv_sum ).
WRITE: / 'Sum:', lv_sum.
" Changing
lv_num = 4.
lo_math->square_number( CHANGING cv_num = lv_num ).
WRITE: / 'Square:', lv_num.
" Returning
lv_pi = lo_math->get_pi( ).
WRITE: / 'Value of PI:', lv_pi.
Output:
d) Event Handling:
Events are signals raised by an object to notify other objects that something has occurred. It allows one object to trigger an action and another object to respond to it automatically. In simple words, An event is a signal raised by one object, and a handler method reacts to that signal.
Event handling is commonly used in:
ALV reports.
GUI programming.
Interactive applications.
Framework-based developments.
CLASS zcl_student DEFINITION.
PUBLIC SECTION.
EVENTS percentage_changed
EXPORTING VALUE(new_percentage) TYPE char5.
METHODS set_percentage IMPORTING iv_percentage TYPE char5.
ENDCLASS.
CLASS zcl_student IMPLEMENTATION.
METHOD set_percentage.
RAISE EVENT percentage_changed EXPORTING new_percentage = iv_percentage.
ENDMETHOD.
ENDCLASS.
CLASS zcl_event_handler DEFINITION.
PUBLIC SECTION.
METHODS on_percentage_changed
FOR EVENT percentage_changed OF zcl_student
IMPORTING new_percentage.
ENDCLASS.
CLASS zcl_event_handler IMPLEMENTATION.
METHOD on_percentage_changed.
WRITE: / 'Percentage updated to:', new_percentage.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_student) = NEW zcl_student( ).
" Create a handler
DATA(lo_handler) = NEW zcl_event_handler( ).
SET HANDLER lo_handler->on_percentage_changed FOR lo_student.
lo_student->set_percentage( iv_percentage = '90.40' ).
Output:
Mastering these OOPS concepts is essential for every ABAP developer aiming to build scalable and future-ready SAP applications.
Think Object-Oriented, Code Smarter in ABAP! Try using them in your next ABAP project!
Thank you for reading. Stay tuned for more ABAP insights.
