Skip to main content

Module 11: ABAP Objects (OOP – Core)

Object-Oriented Programming (OOP) is the foundation of modern ABAP development.
From CDS consumption to RAP, SAP expects ABAP developers to think in objects, responsibilities, and behavior.

This module builds the core OOP mental model, which will be extended in the next module.


1. Why Object-Oriented ABAP?

Procedural ABAP Problems

  • Tight coupling
  • Global data dependency
  • Poor testability
  • Hard to extend

OOP Solves This By

  • Encapsulation of data + logic
  • Clear responsibilities
  • Reusability
  • Easier testing and extension
SAP Direction

All modern SAP frameworks (RAP, OData, CDS consumption) are OO-first.


2. Classes & Objects (Core Concept)

2.1 Class (Blueprint)

A class defines:

  • What data exists → attributes
  • What behavior exists → methods
CLASS zcl_employee DEFINITION.
PUBLIC SECTION.
METHODS display.
ENDCLASS.

2.2 Object (Runtime Instance)

An object is a runtime instance of a class.

DATA(lo_emp) = NEW zcl_employee( ).
Mental Model
  • Class → Design

  • Object → Real entity in memory

3. Attributes & Methods (State vs Behavior)

3.1 Attributes (State)

Attributes store object state.

CLASS zcl_employee DEFINITION.
PRIVATE SECTION.
DATA gv_name TYPE string.
ENDCLASS.
Encapsulation Rule

Attributes should almost always be PRIVATE.

3.2 Methods (Behavior)

Methods operate on object state.

CLASS zcl_employee DEFINITION.
PUBLIC SECTION.
METHODS:
set_name IMPORTING iv_name TYPE string,
get_name RETURNING VALUE(rv_name) TYPE string.
ENDCLASS.
CLASS zcl_employee IMPLEMENTATION.
METHOD set_name.
gv_name = iv_name.
ENDMETHOD.

METHOD get_name.
rv_name = gv_name.
ENDMETHOD.
ENDCLASS.
Clean ABAP Principle

Expose behavior, not data.

4. Constructors (Object Initialization)

Constructors ensure objects are always valid after creation.

4.1 Instance Constructor

Executed when NEW is called.

CLASS zcl_employee DEFINITION.
PUBLIC SECTION.
METHODS constructor IMPORTING iv_name TYPE string.
PRIVATE SECTION.
DATA gv_name TYPE string.
ENDCLASS.
CLASS zcl_employee IMPLEMENTATION.
METHOD constructor.
gv_name = iv_name.
ENDMETHOD.
ENDCLASS.

Usage:

DATA(lo_emp) = NEW zcl_employee( iv_name = 'Yogesh' ).
Design Rule

Use constructors to enforce mandatory data.

4.2 Static Constructor

Runs once per class load.

CLASS zcl_app_config DEFINITION.
PUBLIC SECTION.
CLASS-DATA gv_version TYPE string.
CLASS-METHODS class_constructor.
ENDCLASS.
CLASS zcl_app_config IMPLEMENTATION.
METHOD class_constructor.
gv_version = '2.0'.
ENDMETHOD.
ENDCLASS.
Use Case

Static constructor → configuration, constants, cache initialization.

5. Visibility Sections (Encapsulation)

ABAP supports three visibility levels.

VisibilityMeaning
PUBLICAccessible everywhere
PROTECTEDSubclasses only
PRIVATEOnly within class
CLASS zcl_example DEFINITION.
PUBLIC SECTION.
METHODS public_method.
PROTECTED SECTION.
METHODS protected_method.
PRIVATE SECTION.
METHODS private_method.
ENDCLASS.
Interview Favorite

Encapsulation is the most asked OOP concept.

6. Static vs Instance Components

6.1 Instance Components

Belong to each object.

DATA gv_name TYPE string.

Access:

lo_emp->get_name( ).

6.2 Static Components

Belong to class itself (shared).

CLASS-DATA gv_count TYPE i.
CLASS-METHODS increment.

Access:

zcl_counter=>increment( ).

Comparison

AspectInstanceStatic
MemoryPer objectShared
Access->=>
ScopeObject-specificGlobal-like
Overuse Warning

Static data behaves like global variables. Use sparingly.

7. Short Preview: Next Module Concepts (Type Theory)

The next module builds on top of this foundation.

7.1 Inheritance (Preview)

CLASS zcl_person DEFINITION.
PUBLIC SECTION.
METHODS speak.
ENDCLASS.
CLASS zcl_employee DEFINITION
INHERITING FROM zcl_person.
ENDCLASS.

👉 IS-A relationship (Employee is a Person)

7.2 Interfaces (Preview)

INTERFACE zif_payable.
METHODS calculate_pay.
ENDINTERFACE.
CLASS zcl_employee DEFINITION.
PUBLIC SECTION.
INTERFACES zif_payable.
ENDCLASS.

👉 CAN-DO relationship

7.3 Polymorphism (Preview)

DATA lo_person TYPE REF TO zcl_person.
lo_person = NEW zcl_employee( ).
lo_person->speak( ).

👉 Same reference, different behavior

Why This Matters

RAP, BAdIs, and frameworks rely heavily on polymorphism.

  1. Common Beginner Mistakes
Avoid These
  • PUBLIC attributes

  • Logic-heavy constructors

  • Overusing static methods

  • Mixing procedural and OO styles

  • Ignoring encapsulation

9. Summary

  • Classes define structure + behavior

  • Objects are runtime instances

  • Attributes represent state

  • Methods represent behavior

  • Constructors enforce valid objects

  • Visibility ensures encapsulation

  • Static vs instance must be chosen carefully

10. Practice Exercises

  • Create a class with private attributes and public methods.

  • Enforce mandatory input via constructor.

  • Add a static counter to track object creation.

  • Identify where static data should NOT be used.

  • Refactor a procedural logic block into OO.

11. What’s Next?

➡️ Module 12: OOP Advanced Concepts

  • Inheritance

  • Interfaces

  • Polymorphism

  • Abstract classes

  • SOLID principles (ABAP view)

Learning Tip

If Module 11 is strong, Module 12 becomes intuitive instead of confusing.