Inheritance in SAP ABAP explained with an example….

Akshay Ingalhalli
3 min readNov 22, 2020

--

Inheritance is a concept in Object Oriented Programming (OOP) wherein one class inherits(derives) the properties or components of another class. The class that inherits is called the child class or sub-class and the class being inherited is called the parent class or super class.

Before diving into coding, it is important that we understand, in theory, what inheritance means.

A father has two children. Let us call them Child A and Child B(apologies….I am really bad at coming up with names. Feel free to add some names in the comments section and I will pick the best ones).From a legal point of view, inheritance is defined as transferring property, rights, debts and titles upon the death of a person to his legal heir(s).Let’s consider the father lives to a ripe old age and when his time has come, he leaves a will stating the properties or money to be transferred to each of his children. Assuming the children have all grown up and have an income of their own and their own properties, this inheritance from their father will be added to their existing properties or money. I hope this has given us a brief picture as to what Inheritance means. Let us now look at inheritance from a coding perspective.

The following code is written in SAP ABAP(Advanced Business Application Programming) which is the proprietary language of SAP. In order to understand the following code it is essential that the reader know the basics of object oriented programming such as Class ,Object ,Method and of course the syntax of ABAP programming.

REPORT  ZAKS_SUPER_SUB_CLASS.

CLASS PARENT_CLASS DEFINITION.
PUBLIC SECTION.
METHODS:ADDITION
IMPORTING
IM_A TYPE I
IM_B TYPE I
EXPORTING
EX_VAL TYPE I.

ENDCLASS.

CLASS CHILD_CLASS DEFINITION INHERITING FROM PARENT_CLASS.
PUBLIC SECTION.
METHODS:ADDITION REDEFINITION.
METHODS:MULTIPLICATION
IMPORTING
IM_C TYPE I
IM_D TYPE I
EXPORTING
EX_VAL2 TYPE I.

ENDCLASS.

CLASS PARENT_CLASS IMPLEMENTATION.

METHOD ADDITION.
EX_VAL = IM_A + IM_B.
ENDMETHOD.

ENDCLASS.

CLASS CHILD_CLASS IMPLEMENTATION.

METHOD ADDITION.
EX_VAL = IM_A + IM_B + 60.
ENDMETHOD.

METHOD MULTIPLICATION.
EX_VAL2 = IM_C * IM_D.
ENDMETHOD.

ENDCLASS.

PARAMETERS:P_A TYPE I,
P_B TYPE I.

START-OF-SELECTION.
DATA:LO_PARENT TYPE REF TO PARENT_CLASS,
LO_CHILD TYPE REF TO CHILD_CLASS.
DATA:P_ADD TYPE I,
C_ADD TYPE I,
C_MUL TYPE I.

CREATE OBJECT LO_PARENT.
CREATE OBJECT LO_CHILD.

CALL METHOD LO_PARENT->ADDITION
EXPORTING
IM_A = P_A
IM_B = P_B
IMPORTING
EX_VAL = P_ADD.

CALL METHOD LO_CHILD->ADDITION
EXPORTING
IM_A = P_A
IM_B = P_B
IMPORTING
EX_VAL = C_ADD.

CALL METHOD LO_CHILD->MULTIPLICATION
EXPORTING
IM_C = P_A
IM_D = P_B
IMPORTING
EX_VAL2 = C_MUL.


WRITE:/5 'The result of addition operation of Parent Class:', P_ADD,
/ 'The result of addition operation of Child Class', C_ADD,
/ 'The Multiplication Operation of Child Class:', C_MUL.

The sub-class(Child class) LO_CHILD has inherited the property (in this case the method Addition) of LO_PARENT(super-class or Parent class). However, in the child class LO_CHILD, a small modification has been made by using the keyword REDEFINITION. This keyword allows us to modify the method Addition of the class LO_PARENT.As shown in the above example, the integer 60 has been added to the method Addition of LO_CHILD(So that the result of the addition operation of both Classes are different.)In addition to the inherited method Addition, LO_CHILD has its own method Multiplication.

To encapsulate, Inheritance is a very important concept in Object Oriented Programming which allows reusability and readability. When the child class inherits the properties of parent class, there is no need to write the same code again in child class. This makes it easier to reuse the code which in turn leads to lesser coding effort and code becomes much more readable.

--

--

Akshay Ingalhalli
Akshay Ingalhalli

Written by Akshay Ingalhalli

ERP guy| Obsessive enthusiast| Gourmet| Cafephile

Responses (1)