Object Oriented Programming in SAP ABAP - OOABAP


What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects." These objects are instances of classes, which are blueprints defining the properties (attributes) and behaviors (methods) that the objects can have. OOP is designed to make code more modular, reusable, and easier to maintain by organizing it around objects rather than actions.

Key Concepts of OOP

1. Class:

   A class is a blueprint or template that defines the structure and behavior (methods) of objects. For example, you can have a class called `Car` that defines attributes like `color`, `make`, and `model`, and methods like `start_engine()` or `stop_engine()`.

   Code:

CLASS Car DEFINITION.

  PUBLIC SECTION.

    DATA: color TYPE string,

          make  TYPE string,

          model TYPE string.

    METHODS: start_engine,

             stop_engine.

ENDCLASS.

 2. Object:

   An object is an instance of a class. When a class is defined, no memory is allocated until an object is created from it. Each object can have different values for the attributes defined by its class.

   Code:

   DATA my_car TYPE REF TO Car.

  CREATE OBJECT my_car.

3. Encapsulation:

   Encapsulation is the concept of hiding the internal state of an object and requiring all interaction to be performed through the object's methods. This keeps the data safe from unintended interference and misuse.

 Code:     

 CLASS Car DEFINITION.

  PRIVATE SECTION.

    DATA: fuel_level TYPE i.

  PUBLIC SECTION.

    METHODS: set_fuel_level IMPORTING new_level TYPE i,

             get_fuel_level RETURNING VALUE(level) TYPE i.

ENDCLASS.

CLASS Car IMPLEMENTATION.

  METHOD set_fuel_level.

    fuel_level = new_level.

  ENDMETHOD.

  METHOD get_fuel_level.

    WRITE: / fuel_level.

  ENDMETHOD.

ENDCLASS.

4. Inheritance:

   Inheritance allows a new class to inherit the properties and methods of an existing class. The new class, called a subclass, can add new properties or methods or override existing ones.

   Code:

CLASS car DEFINITION.

  PUBLIC SECTION.

    METHODS: set_fuel_level IMPORTING new_level TYPE i,

      get_fuel_level RETURNING VALUE(level) TYPE i.

  PRIVATE SECTION.

    DATA: fuel_level TYPE i.

ENDCLASS.

CLASS car IMPLEMENTATION.

  METHOD set_fuel_level.

    fuel_level = new_level.

  ENDMETHOD.

  METHOD get_fuel_level.

    WRITE: / fuel_level.

  ENDMETHOD.

ENDCLASS.

CLASS electriccar DEFINITION INHERITING FROM car.

  PUBLIC SECTION.

    METHODS charge_battery.

ENDCLASS.

CLASS electriccar IMPLEMENTATION.

  METHOD charge_battery.

    " Charging logic here

  ENDMETHOD.

ENDCLASS.   

5. Polymorphism:

   Polymorphism allows objects of different classes to be treated as objects of a common superclass. It is mainly achieved through method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass.

   Code:

CLASS vehicle DEFINITION.

  PUBLIC SECTION.

    METHODS start_engine.

ENDCLASS.

CLASS vehicle IMPLEMENTATION.

  METHOD start_engine.

    WRITE: 'Starting generic vehicle engine.'.

  ENDMETHOD.

ENDCLASS.

CLASS car DEFINITION INHERITING FROM vehicle.

  PUBLIC SECTION.

    METHODS start_engine REDEFINITION. "Overriding

ENDCLASS.

CLASS car IMPLEMENTATION.

  METHOD start_engine.

    WRITE: 'Starting car engine.'.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

  DATA my_car TYPE REF TO car.

  CREATE OBJECT my_car.

  CALL METHOD my_car->start_engine.

Output:

Starting car engine.

Why Use OOP in ABAP?

Modularity: Code is organized into discrete objects, making it easier to manage and maintain.

Reusability: Objects and classes can be reused across different programs, reducing redundancy.

Flexibility: Inheritance and polymorphism allow for more flexible and adaptable code.

Maintainability: Encapsulation ensures that the internal workings of an object are hidden, leading to fewer side effects when making changes.

OOP in ABAP provides a robust framework for building complex applications that are easier to manage and maintain over time. By understanding and applying the core principles of OOP—classes, objects, encapsulation, inheritance, and polymorphism—you can write more efficient and scalable ABAP code.

1 Comments

Previous Post Next Post