Object-Oriented Programming in ABAP

August 30, 2024

Understanding Object-Oriented Programming (OOP) in ABAP

Object-Oriented Programming (OOP) is a programming paradigm that uses ‘objects’ to design applications and computer programs. It utilizes several key concepts, such as encapsulation, inheritance, and polymorphism. In this lesson, we will explore how these principles are applied in ABAP.

What is a Class?

A class in ABAP is a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that the objects created from the class will have. To define a class, we use the CLASS statement.

CLASS my_class DEFINITION.
  PUBLIC SECTION.
    METHODS: my_method.
  PRIVATE SECTION.
    DATA: my_attribute TYPE i.
ENDCLASS.

Creating Objects

Once you have defined a class, you can create objects from it. This is done using the CREATE OBJECT statement.

DATA: my_object TYPE REF TO my_class.
CREATE OBJECT my_object.

Encapsulation

Encapsulation is the bundling of data (attributes) and methods that operate on that data into a single unit, or class. It restricts direct access to some of an object’s components, which is a means of preventing unintended interference and misuse of methods and data. In ABAP, you can control access to class components using PUBLIC, PROTECTED, and PRIVATE keywords.

Inheritance

Inheritance allows a class (the child class) to inherit the properties and methods of another class (the parent class). This promotes code reusability. You define inheritance using the INHERITING FROM clause.

CLASS my_child_class INHERITING FROM my_class DEFINITION.
  PUBLIC SECTION.
    METHODS: my_child_method.
ENDCLASS.

Polymorphism

Polymorphism allows methods to do different things based on the object it is acting upon, even though they share the same name. In ABAP, you can achieve polymorphism using method overriding and interfaces.

Method Overriding

When a child class defines a method with the same name as a method in its parent class, it overrides the parent class method. This allows the child class to provide a specific implementation of the method.

CLASS my_child_class DEFINITION.
  PUBLIC SECTION.
    METHODS: my_method REDEFINITION.
ENDCLASS.

Interfaces

Interfaces define a set of methods that a class must implement, without providing the implementation itself. This allows different classes to be treated as the same type if they implement the same interface.

INTERFACE my_interface.
  METHODS: my_interface_method.
ENDINTERFACE.

Putting It All Together

Here’s a simple example that demonstrates the concepts of classes, objects, inheritance, and polymorphism in ABAP:

CLASS animal DEFINITION.
  PUBLIC SECTION.
    METHODS: speak.
ENDCLASS.

CLASS animal IMPLEMENTATION.
  METHOD speak.
    WRITE 'Animal speaks'.
  ENDMETHOD.
ENDCLASS.

CLASS dog INHERITING FROM animal DEFINITION.
  PUBLIC SECTION.
    METHODS: speak REDEFINITION.
ENDCLASS.

CLASS dog IMPLEMENTATION.
  METHOD speak.
    WRITE 'Dog barks'.
  ENDMETHOD.
ENDCLASS.

DATA: my_animal TYPE REF TO animal,
      my_dog TYPE REF TO dog.

CREATE OBJECT my_animal.
CREATE OBJECT my_dog.

my_animal->speak( ).  " Output: Animal speaks
my_dog->speak( ).    " Output: Dog barks

Conclusion

In this lesson, we have covered the fundamental principles of Object-Oriented Programming in ABAP. By understanding classes, objects, inheritance, and polymorphism, you can write more organized and reusable code. As you continue your learning journey, practice implementing these concepts in your own ABAP programs to solidify your understanding.