
OOPS Programming in SAP ABAP: Concepts, Features, and Real-Time Examples
SAP ABAP has evolved far beyond its procedural origins. Since the advent of the OOP programming feature within SAP ABAP, developers discovered a revolutionary model that is akin to the real world where behavior and data are integrated and reuse is built into the enterprise application, and it scales easily.
No matter if you're just beginning your journey into SAP developing or a seasoned consultant who is modernizing older code, understanding OOPS programming in SAP ABAP is no longer a matter of choice -- it's a prerequisite. Technologies such as BAdIs, ALV, Web Dynpro, classes along with SAP Fiori extension all require an in-depth understanding of SAP ABAP OOPs.
This guide takes you through each essential concept as well as practical examples to assist you in applying OOPs in SAP ABAP on real-time tasks.
What Is Object-Oriented Programming in SAP ABAP?
Object-oriented programming (OOP) in SAP ABAP is a model of programming which organizes code in the form of objects instead of procedures. An object is an instance of a class -- the blueprint which defines the data (attributes) as well as behavior (methods) of the object.
SAP implemented OOPs concepts into ABAP when it released SAP Basis 4.0 and it has grown significantly in subsequent versions. Nowadays, ABAP Objects (as it's officially known) constitutes the foundation of modern SAP development, which includes SAP customizations to S/4HANA.
Core Pillars of OOPs Programming in SAP ABAP
The fundamental principles of OOPs within SAP ABAP coincide with standard object-oriented theories, however they are implemented inside ABAP's distinct syntax and SAP's runtime environments. The four major pillars are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
Let's explore each one in detail and with real-time ABAP code examples.
1. Classes and Objects – The Building Blocks
Before you dive into the four main pillars, it is essential to understand classes as well as objects that form the basis of OOP concepts in SAP ABAP.
What Is a Class?
A class is a blueprint that defines:
- Attributes – Variables or data that store the state.
- Methods – Procedures or functions that define the behavior.
- Events – Triggers that objects can raise.
- Interfaces – Contracts that classes are able to implement.
Classes in ABAP can be described in two different ways:
- Global Class – created using SE24 (Class Builder) and reused across the system.
- Local Class – defined within the program using CLASS ... ENDCLASS.
Real-Time Example: Defining a Class
CLASS lcl_employee DEFINITION.
PUBLIC SECTION.
DATA: lv_emp_id TYPE i,
lv_emp_name TYPE string.
METHODS: set_details IMPORTING iv_id TYPE i
iv_name TYPE string,
display_details.
ENDCLASS.
CLASS lcl_employee IMPLEMENTATION.
METHOD set_details.
lv_emp_id = iv_id.
lv_emp_name = iv_name.
ENDMETHOD.
METHOD display_details.
WRITE: / 'Employee ID:', lv_emp_id,
/ 'Employee Name:', lv_emp_name.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: lo_employee TYPE REF TO lcl_employee.
CREATE OBJECT lo_employee.
lo_employee->set_details( iv_id = 101 iv_name = 'Rahul Sharma' ).
lo_employee->display_details( ).
In this instance, lcl_employee is the class and lo_employee is the object (an instance of this class). This is the simplest use of OOPs programming within SAP ABAP.
2. Encapsulation – Protecting Your Data
Encapsulation is the process of hiding the internal state of an object and requiring each interaction to be executed using defined methods. In SAP ABAP OOPs this is accomplished using visibility sections:
| Section | Access Level |
|---|---|
| PUBLIC | Accessible from any location |
| PROTECTED | Accessible within the class as well as subclasses |
| PRIVATE | Only accessible within the class |
Real-Time Example: Encapsulation in SAP ABAP
CLASS lcl_bank_account DEFINITION.
PUBLIC SECTION.
METHODS: deposit IMPORTING iv_amount TYPE p DECIMALS 2,
get_balance RETURNING VALUE(rv_balance) TYPE p DECIMALS 2.
PRIVATE SECTION.
DATA: lv_balance TYPE p DECIMALS 2.
ENDCLASS.
CLASS lcl_bank_account IMPLEMENTATION.
METHOD deposit.
lv_balance = lv_balance + iv_amount.
ENDMETHOD.
METHOD get_balance.
rv_balance = lv_balance.
ENDMETHOD.
ENDCLASS.
In this case, lv_balance is kept private and external programs are unable to directly alter it. This is one of the strengths of OOPs in SAP ABAP -- it safeguards crucial business information from accidental changes that are detrimental to payroll as well as finance and banking SAP modules.
3. Inheritance – Reuse and Extend
Inheritance allows a child class (subclass) to inherit attributes as well as methods of the parent class (superclass). This is among the most effective OOPs concepts available in SAP ABAP to reduce the amount of code duplicated.
In ABAP, inheritance is achieved through the INHERITING FROM keyword.
Real-Time Example: Inheritance in SAP ABAP
CLASS lcl_vehicle DEFINITION.
PUBLIC SECTION.
DATA: lv_brand TYPE string.
METHODS: start_engine.
ENDCLASS.
CLASS lcl_vehicle IMPLEMENTATION.
METHOD start_engine.
WRITE: / 'Engine started for', lv_brand.
ENDMETHOD.
ENDCLASS.
CLASS lcl_car DEFINITION INHERITING FROM lcl_vehicle.
PUBLIC SECTION.
METHODS: open_sunroof.
ENDCLASS.
CLASS lcl_car IMPLEMENTATION.
METHOD open_sunroof.
WRITE: / 'Sunroof opened for', lv_brand.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA: lo_car TYPE REF TO lcl_car.
CREATE OBJECT lo_car.
lo_car->lv_brand = 'Toyota'.
lo_car->start_engine( ).
lo_car->open_sunroof( ).
lcl_car inherits lv_brand and start_engine from lcl_vehicle and adds its own method, open_sunroof. This reflects real SAP situations, like a base document class that is extended to include lcl_sales_order and lcl_purchase_order classes.
4. Polymorphism – One Interface, Many Forms
Polymorphism describes how the same method name behaves differently based on the object calling it. In OOPs programming within SAP ABAP this is accomplished by method overriding as well as interfaces.
Real-Time Example: Polymorphism via Method Overriding
CLASS lcl_shape DEFINITION.
PUBLIC SECTION.
METHODS: calculate_area.
ENDCLASS.
CLASS lcl_shape IMPLEMENTATION.
METHOD calculate_area.
WRITE: / 'Calculating area of a general shape.'.
ENDMETHOD.
ENDCLASS.
CLASS lcl_circle DEFINITION INHERITING FROM lcl_shape.
PUBLIC SECTION.
METHODS: calculate_area REDEFINITION.
ENDCLASS.
CLASS lcl_circle IMPLEMENTATION.
METHOD calculate_area.
WRITE: / 'Calculating Area of a Circle (π x R²)'.
ENDMETHOD.
ENDCLASS.
CLASS lcl_rectangle DEFINITION INHERITING FROM lcl_shape.
PUBLIC SECTION.
METHODS: calculate_area REDEFINITION.
ENDCLASS.
CLASS lcl_rectangle IMPLEMENTATION.
METHOD calculate_area.
WRITE: / 'Calculating Area of the Rectangle: l x b'.
ENDMETHOD.
ENDCLASS.
Both lcl_circle and lcl_rectangle override calculate_area. When called at runtime on various object references, the right version is executed -- this is runtime polymorphism, an essential component of SAP OOPs ABAP.
5. Abstraction – Hiding Complexity
Abstraction is focused on revealing only what is required and omitting the more complex details of implementation. In OOPs programming within SAP ABAP, abstraction is implemented using abstract classes as well as interfaces.
Abstract Classes
Abstract classes cannot be instantiated directly. They function as an outline for subclasses.
CLASS lcl_report DEFINITION ABSTRACT.
PUBLIC SECTION.
METHODS: generate_report ABSTRACT.
ENDCLASS.
CLASS lcl_sales_report DEFINITION INHERITING FROM lcl_report.
PUBLIC SECTION.
METHODS: generate_report REDEFINITION.
ENDCLASS.
CLASS lcl_sales_report IMPLEMENTATION.
METHOD generate_report.
WRITE: / 'Generating Sales Report...'.
ENDMETHOD.
ENDCLASS.
Interfaces in SAP ABAP
Interfaces define a contract -- a set of procedures which any class that implements it must provide. They are essential to OOP principles in SAP ABAP and are widely used in standard SAP to implement BAdIs as well as ALV.
INTERFACE lif_printable.
METHODS: print_document.
ENDINTERFACE.
CLASS lcl_invoice DEFINITION.
PUBLIC SECTION.
INTERFACES: lif_printable.
ENDCLASS.
CLASS lcl_invoice IMPLEMENTATION.
METHOD lif_printable~print_document.
WRITE: / 'Printing Invoice...'.
ENDMETHOD.
ENDCLASS.
Interface methods are referenced using the tilde (~) operator -- an exclusive syntax feature of ABAP OOPs.
Static vs. Instance Components in SAP ABAP OOPs
Understanding the distinction between static components and instance components is among the essential notions of OOPs within SAP ABAP that developers typically employ.
| Feature | Instance Component | Static Component |
|---|---|---|
| Belongs to | Each object separately | The class itself |
| Accessed via | Object reference ( -> ) | Class name ( => ) |
| Keyword | Default | CLASS-DATA, CLASS-METHODS |
CLASS lcl_counter DEFINITION.
PUBLIC SECTION.
CLASS-DATA: gv_count TYPE i.
CLASS-METHODS: increment.
ENDCLASS.
CLASS lcl_counter IMPLEMENTATION.
METHOD increment.
gv_count = gv_count + 1.
WRITE: / 'Count:', gv_count.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
lcl_counter=>increment( ).
lcl_counter=>increment( ).
Static methods are often employed for counters, utility functions, and factory processes in real SAP projects.
Events in SAP ABAP OOPs
Events are an essential component of OOPs in SAP ABAP that allows loose coupling between different objects. One object triggers an event and another handles it.
CLASS lcl_publisher DEFINITION.
PUBLIC SECTION.
EVENTS: data_changed.
METHODS: trigger_event.
ENDCLASS.
CLASS lcl_subscriber DEFINITION.
PUBLIC SECTION.
METHODS: on_data_changed
FOR EVENT data_changed OF lcl_publisher.
ENDCLASS.
Events are extensively utilized throughout the SAP Business Object framework, workflow triggers, and customized ALV grid-based interactions.
Real-Time Use Cases of OOPs in SAP ABAP Projects
Knowing the theory is only the first step. Here's how OOP programming in SAP ABAP corresponds to actual SAP project scenarios:
- ALV Reports Using OOPs – The CL_GUI_ALV_GRID class is a regular SAP global class. Developers can instantiate it, create field catalogs, call display methods and handle events such as DOUBLE_CLICK -- all using pure OOPs.
- BAdI Implementations – BAdIs that are part of SAP S/4HANA are completely interface-based. Each BAdI implementation needs to create a class that implements the interface of the given BAdI -- directly applying OOPs concepts within SAP ABAP.
- Factory Design Patterns – Senior ABAP developers use factory methods to return appropriate subclass objects based on input parameters -- for example, returning a lcl_domestic_tax or lcl_international_tax object based on the country key.
- Singleton Pattern for Database Access – A singleton class makes sure that only one instance is responsible for every database call during a program's execution, optimizing performance when processing large-volume SAP batch jobs.
Key Differences: Procedural ABAP vs. OOPs ABAP
| Feature | Procedural ABAP | OOPs ABAP |
|---|---|---|
| Code structure | Subroutines, Function Modules | Methods and classes |
| Data handling | Global variables | Encapsulated attributes |
| Reusability | Limited | High via inheritance/interfaces |
| Maintenance | More difficult to handle | Modular and adaptable |
| SAP S/4HANA compatibility | Legacy | The recommended approach |
Tips to Master OOPs Concepts in SAP ABAP
- Begin with SE24 – Learn about the standard SAP global classes such as CL_SALV_TABLE and CL_GUI_ALV_GRID to observe real-world OOP implementation.
- Learn locally first – Write small programs that use local class definitions before starting to work with global classes.
- Examine Standard BAdIs – Reviewing how SAP S/4HANA BAdIs utilize interfaces will increase your understanding of abstraction and polymorphism.
- Utilize ABAP Object Services – Persistence services and query services are based entirely on OOPs.
- Join a structured course – Learning with guidance from experienced mentors can speed up your learning substantially.
Why Learn OOPs Programming in SAP ABAP with ERPVITS?
At ERPVITS, our SAP ABAP training program is designed around relevant industry projects which reflect the situations you'll face during your workday. Our instructors are experienced SAP consultants with more than 10 years of experience in real-world projects across various industries.
The ABAP OOPs course covers:
- The fundamental OOP concepts in SAP ABAP built from scratch
- Hands-on code labs using BAdIs, ALVs, and custom-designed applications
- Real-time project scenarios derived from SAP SD, MM, HR, and FI modules
- Interview preparation using OOPs-focused ABAP questions
- Lifetime access to recordings and course material
Whether you're interested in online or self-paced instruction, ERPVITS has a program suited to your needs and schedule.
Conclusion
OOPs programming within SAP ABAP is not a fad -- it's the standard for creating robust, maintenance-friendly, and scalable SAP solutions for the S/4HANA era. From inheritance and encapsulation to abstraction and polymorphism, the core OOP concepts in SAP ABAP give you the tools to create enterprise-grade software that has stood the test of time.
Mastering SAP ABAP OOPs opens the door to more advanced development tasks like BAdI implementations, Fiori backend services, SmartForms through classes, and much more. The time spent studying OOP programming in SAP ABAP results in better project performance, faster debugging, as well as greater earnings as an SAP professional.
Are you ready to improve your ABAP skills to the next level? Explore ERPVITS SAP ABAP OOPs Training and learn from the experts who have created SAP solutions for Fortune 500 companies.