Skip to main content

SAP ABAP DEVELOPER OOPS Basics for Beginners – Concepts, Examples & Real-Time Programs

Complete Guide to SAP ABAP OOPS: Classes, Methods, Constructors, Inheritance, Polymorphism & Interfaces.

6 BULLET POINTS – WHAT YOU WILL LEARN IN THIS BLOG.

Each point includes a high-ranking SEO keyword related to OOPS ABAP.

  1. Understanding SAP ABAP OOPS concepts such as Encapsulation, Abstraction, Inheritance, and Polymorphism for real-time development.
  2. Learning ABAP Classes and Objects with Local Class and Global Class examples using SE24.
  3. Working with ABAP Methods including Instance Methods, Static Methods, and CLASS-METHODS using MARA table programs.
  4. Implementing ABAP Constructors (CONSTRUCTOR & CLASS_CONSTRUCTOR) to auto-initialize data in OOPS-based programs.
  5. Building SAP ABAP Inheritance and Polymorphism programs with method redefinition and subclass behaviour.
  6. Using ABAP Interfaces in OOPS with real-time examples on IF_DISP and MARA table data display.
  7. REAL-TIME SAP ABAP OOPS INTERVIEW QUESTIONS & ANSWERS

SAP ABAP OOPS Basics for Beginners – Complete Guide with Real-Time Examples

Object-Oriented Programming (OOPS) in SAP ABAP is one of the most powerful programming models used in real-time business applications. It helps developers write structured, reusable, and maintainable code.

This guide explains all core OOPS concepts with simple definitions, syntax, and real-time MARA table programs.


What is OOPS in SAP ABAP?

OOPS (Object-Oriented Programming) in ABAP is a programming approach that uses objects, classes, methods, and attributes to design applications in a structured way.


Key Features of ABAP OOPS

  1. Encapsulation – Protects data by restricting direct access.

  2. Abstraction – Shows only necessary details to the user.

  3. Inheritance – A class can acquire the properties of another class.

  4. Polymorphism – Same method name behaves differently in different classes.


1. Classes in SAP ABAP OOPS

Classes are blueprints used to create objects at runtime.

Types of Classes

  1. Local Class – Defined inside the program.
  2. Global Class – Created using SE24 (Class Builder).


📌 Local Class Syntax

CLASS <class_name> DEFINITION. ... ENDCLASS. CLASS <class_name> IMPLEMENTATION. ... ENDCLASS.

2. Visibility Sections

Every class contains three visibility sections:

  1. PUBLIC SECTION – Accessible to all.
  2. PROTECTED SECTION – Accessible to subclass only.
  1. PRIVATE SECTION – Accessible inside same class only.

Visibility must be written in the correct order:

  1. PUBLIC
  2. PROTECTED
  3. PRIVATE


3. Attributes in ABAP OOPS

Attributes represent data of the class.

Types of Attributes

  1. Instance Attributes

DATA <attr_name> TYPE <type>.
  1. Static Attributes

CLASS-DATA <attr_name> TYPE <type>.

4. Methods in ABAP OOPS

Methods define the behavior of a class.

Types of Methods:

  • Instance Methods (METHODS)

  • Static Methods (CLASS-METHODS)


Real-Time Program 1: Display MARA Data using OOPS

REPORT ZMM_OOP_1. CLASS C1 DEFINITION. PUBLIC SECTION. DATA: wa_mara TYPE mara, it_mara TYPE STANDARD TABLE OF mara. METHODS: get_data, display_data. ENDCLASS. CLASS C1 IMPLEMENTATION. METHOD get_data. SELECT * FROM mara INTO TABLE it_mara UP TO 10 ROWS. ENDMETHOD. METHOD display_data. LOOP AT it_mara INTO wa_mara. WRITE: / wa_mara-matnr, wa_mara-ersda, wa_mara-ernam. ENDLOOP. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA obj TYPE REF TO C1. CREATE OBJECT obj. obj->get_data( ). obj->display_data( ).

5. Constructors in SAP ABAP OOPS

Instance Constructor

Used to initialize instance attributes when object is created.

METHOD constructor.

Class Constructor (Static Constructor)

Runs automatically when class is first accessed.

CLASS_CONSTRUCTOR.

Constructor Program Example

CLASS C1 DEFINITION. PUBLIC SECTION. DATA: wa_mara TYPE mara, it_mara TYPE STANDARD TABLE OF mara. METHODS: constructor, display_data. ENDCLASS. CLASS C1 IMPLEMENTATION. METHOD constructor. SELECT * FROM mara INTO TABLE it_mara UP TO 10 ROWS. ENDMETHOD. METHOD display_data. LOOP AT it_mara INTO wa_mara. WRITE: / wa_mara-matnr, wa_mara-ersda, wa_mara-ernam. ENDLOOP. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA obj TYPE REF TO C1. CREATE OBJECT obj. obj->display_data( ).

6. Inheritance in SAP ABAP OOPS

Inheritance allows a subclass to access properties of the superclass.

Simple Inheritance Program

CLASS C1 DEFINITION. PUBLIC SECTION. DATA: wa_mara TYPE mara, it_mara TYPE TABLE OF mara. METHODS: get_data. ENDCLASS. CLASS C2 DEFINITION INHERITING FROM C1. PUBLIC SECTION. METHODS: display_data. ENDCLASS. CLASS C1 IMPLEMENTATION. METHOD get_data. SELECT * FROM mara INTO TABLE it_mara UP TO 10 ROWS. ENDMETHOD. ENDCLASS. CLASS C2 IMPLEMENTATION. METHOD display_data. LOOP AT it_mara INTO wa_mara. WRITE: / wa_mara-matnr, wa_mara-ersda, wa_mara-ernam. ENDLOOP. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA obj TYPE REF TO C2. CREATE OBJECT obj. obj->get_data( ). obj->display_data( ).

7. Polymorphism in ABAP OOPS

Polymorphism means same method name behaves differently in different classes using REDEFINITION.


Polymorphism Example

CLASS C2 DEFINITION INHERITING FROM C1. PUBLIC SECTION. METHODS get_data REDEFINITION. ENDCLASS.

8. Interfaces in ABAP OOPS

Interface contains method definitions only.
Classes must provide implementation.


Interface Example

INTERFACE if_disp. METHODS show. ENDINTERFACE. CLASS C1 DEFINITION. PUBLIC SECTION. INTERFACES if_disp. ENDCLASS. CLASS C1 IMPLEMENTATION. METHOD if_disp~show. WRITE: / 'Interface method executed'. ENDMETHOD. ENDCLASS. START-OF-SELECTION. DATA obj TYPE REF TO C1. CREATE OBJECT obj. obj->if_disp~show( ).

9. Static Methods & Static Attributes

Static components are defined with CLASS-DATA and CLASS-METHODS.
Called without creating an object.

CALL METHOD c1=>m1.

Static Method Program

CLASS C1 DEFINITION. PUBLIC SECTION. CLASS-DATA n1 TYPE i. CLASS-METHODS m1. ENDCLASS. CLASS C1 IMPLEMENTATION. METHOD m1. n1 = 10. WRITE: / n1. ENDMETHOD. ENDCLASS. START-OF-SELECTION. c1=>m1( ).

REAL-TIME SAP ABAP OOPS INTERVIEW QUESTIONS & ANSWERS


1️⃣ Question:

What is the difference between Instance Methods and Static Methods in SAP ABAP OOPS? Give a real-time example.

Answer:

  • Instance Methods require object creation using CREATE OBJECT.
    They are used when processing object-specific data, such as WA_MARA and IT_MARA for each object instance.

  • Static Methods do not require object creation.
    They are used when the logic is common for all objects, such as fetching configuration values, constants, or reusable utilities.

Real-Time Example:

  • Instance Method → Fetch MARA data row-by-row for each object.

  • Static Method → Logging, Date Calculation, or a reusable method to read common MARA fields.2️⃣ Question:

Why do we use Constructors in ABAP OOPS? Explain with MARA table example.

Answer:

Constructors automatically execute when an object is created.

  • Instance Constructor initializes object data.

  • Static Constructor initializes class-level data.

Example:

Fetching MARA data in the constructor ensures that:

CREATE OBJECT obj.

automatically loads MARA rows without calling a separate method.

This is useful in real-time when:

  • You must auto-initialize configuration

  • You want default values

  • You want the program to load master data at object creation

3️⃣ Question:

What is the real-time use of Inheritance in SAP ABAP OOPS?

Answer:

Inheritance reuses logic from a parent class.

Real-time scenario:

  • Parent Class → Reads Material Master (MARA)

  • Child Class → Displays or formats MARA data differently (ALV, List, SmartForm, Email)

This helps avoid repeating the SELECT statement in multiple classes.

4️⃣ Question:

How does Polymorphism help in ABAP OOPS? Provide a practical scenario.

Answer:

Polymorphism allows you to define the same method name in multiple classes using REDEFINITION.

Real-time practical use:
Different departments want material data displayed differently:

  • MM Department → Show MATNR, ERSDA

  • PP Department → Show MATNR, MEINS

  • SD Department → Show MATNR, MTART

All classes use the same method name:

GET_DATA( ).

But each class shows different results.

5️⃣ Question:

What is the difference between Local and Global Classes in ABAP OOPS?

Answer:

FeatureLocal ClassGlobal Class
Created InProgram (SE38)Class Builder (SE24)
ScopeProgram-onlySystem-wide available
Use-caseSimple utilities, local logic, reportsReusable logic, BAdIs, Frameworks

Real-time:

  • Global classes are used in Enhancements, BOPF, CDS behavior, WebDynpro, Fiori OData.

  • Local classes are used in Reports & Module Pool.

6️⃣ Question:

What is the purpose of Visibility Sections (Public, rivate, Protected)?

Answer:

  • PUBLIC → Accessed from outside the class (e.g., obj->get_data).

  • PRIVATE → Used inside the same class only (best for sensitive data).

  • PROTECTED → Accessible inside the class and its subclasses (inheritance scenario).

Real-time example ON ALV REPORTS:
PRIVATE section is used to store confidential data like user ID, internal counters, sensitive fields.

7️⃣ Question:

What is an Interface in ABAP and why is it used?

Answer:

An Interface contains method definitions only (no implementation).
Implemented using INTERFACES <intf_name> keyword.

Real-time usage:

  • Standardizing business logic across multiple classes

  • Defining rules (example: material data display rules)

  • Achieving multiple inheritance (ABAP classes cannot inherit from multiple classes)

8️⃣ Question:

When would you prefer Static Attributes over Instance Attributes?

Answer:

Use Static Attributes when:

  • The data must be common to all objects

  • The value must be shared across the entire session

  • You want to avoid repeated initialization

Real-time example:
A static variable storing:

  • Number of times material data was fetched

  • Program execution counter.

  • System-wide default material type (MTART).

9️⃣ Question:

Explain a real-time scenario where Polymorphism helps avoid code duplication.

Answer:

Suppose you have 3 formats for displaying MARA:

ModuleOutput
MMMATNR, ERSDA
SDMATNR, MTART
PPMATNR, MEINS

Each child class redefines DISPLAY_DATA method.

All you do is:

OBJ->DISPLAY_DATA( ).

Depending on the instantiated class, output changes.
This avoids writing separate reports for each format.

🔟 Question:

What is the real-time use of Static Methods when working with MARA table programs?

Answer:

Static Methods are used for:

  • Utility operations

  • Reusable logic

  • No object creation required

Real-time Examples:

  • Static method to validate MATNR format

  • Static method to convert MARA fields

  • Static method to fetch common configuration

  • Displaying MARA data without creating a class object

C1=>M1( ).

NOTE :

* LEARN SAP ABAP ALV REPORTS.

* LEARN ALV REPORTS REUSE_ALV_GRID_DISPLAY PROGRAMS

Comments

Popular posts from this blog

SAP ABAP SE11 Data Dictionary Tutorial | Step-by-Step Explanation with Real-Time Examples

    SAP ABAP Data Dictionary (SE11) – Complete Guide to Tables, Views, Domains & Data Elements    What is the ABAP Data Dictionary? The SAP ABAP data dictionary serves as a primary and structured source of data for creating objects. It is an independent database DDL (Data Definition Language) that primarily deals with creating, editing, and dropping database tables. The data dictionary allows you to define and maintain database-related items. The ABAP data dictionary is readily connected with the ABAP workbench, allowing all of the workbench's components to simply access the definitions contained in the dictionary. Functions of Data Dictionary The important functions of data dictionary objects are as follows. 1. Database tables 2. Domains 3. Data elements 4. Views 5. Search helps 6. Lock Objects 1. Database Tables: –  Database tables are collections of fields that store physical data. It is an object that holds data in the form of rows and columns. So databa...

SAP ABAP ALV Report Using REUSE_ALV_GRID_DISPLAY – Complete Tutorial with Examples

  📘 SAP ABAP ALV Report Using REUSE_ALV_GRID_DISPLAY – Complete Step-by-Step Guide (With Real Examples) SAP ABAP developers frequently use ALV (ABAP List Viewer) to present data in a clean and interactive format. One of the most commonly used ALV function modules in classical reporting is: This function module allows developers to create professional ALV reports with features such as sorting, filtering, totals, layout saving, and exporting to Excel. In this guide, I will explain: What REUSE_ALV_GRID_DISPLAY is How field catalogs work How to build an ALV report using MARC and KNA1 tables Real-time business examples Clean, fully working ABAP code This post is written from my personal learning and hands-on experience while working on SAP ABAP reports. 🧩 What is REUSE_ALV_GRID_DISPLAY? REUSE_ALV_GRID_DISPLAY is a function module-based ALV used in SAP ECC and compatible with S/4HANA systems. It displays internal table data in a grid-style output , which is more user...

SAP ABAP Control Break Statements – Complete Guide with AT FIRST, AT NEW & AT END OF Examples

📘 SAP ABAP Control Break Statements – Complete Guide with Example Learn about Control Break Statements in SAP ABAP including AT FIRST, AT NEW, AT END OF, AT LAST, and ON CHANGE OF. Includes a real-time example program with explanation and output. Perfect for SAP ABAP beginners and developers. SAP ABAP Control Break Statements AT NEW ABAP example AT END OF ABAP tutorial ABAP loop control SAP ABAP report programming AT FIRST, AT LAST in ABAP SAP ABAP internal table sorted 📘 Introduction In SAP ABAP , control break statements are used to trigger events inside a loop . They are essential for creating grouped or hierarchical reports , especially when working with sorted internal tables. There are 5 main control break statements in ABAP: AT FIRST – ENDAT AT NEW – ENDAT AT END OF – ENDAT AT LAST – ENDAT ON CHANGE OF FIELD. SAP ABAP Loop Control Tutorial:  How to Use AT NEW, AT END OF, AT FIRST, AT LAST, & ON CHANGE OF  FIELD. 🧠 Key Control Break Statements Explai...