Super Class in SAP ABAP

by | Mar 27, 2019 | OOABAP

Home » SAP » ABAP » OOABAP » Super Class in SAP ABAP

Preface – This post is part of the Object Oriented ABAP series.

Introduction

One of the three pillars of OOABAP uses this concept of Super class to achieve ‘Inheritance’. Super class allows us to extend that class by creating a subclass of it. This subclass will have all the properties of a super class, plus it can have additional properties as well.

Definition

A class which is having a derived class from it, is known as a ‘Super Class’.

To make a class Super Class, the first step is to uncheck the box indicating Final Class while creating a Class through T-Code SE24 (Class Builder). Because if a class is creating as a Final cannot be extended further, and inheritance cannot be achieved in that case.

If the developer is using this concept of the super class & subclass, then he/she can have all the advantages of ‘Inheritance’.

Program of Super Class in SAP ABAP:

Let’s take an example of this employer class ‘ZCL_EMPLOYER’. This employer class is having one method ‘GET_INCOME’ to get income.

 

CLASS zcl_employer DEFINITION.

PUBLIC SECTION.

METHODS: get_income RETURNING VALUE(income) TYPE F.

PRIVATE SECTION.

DATA: income TYPE F VALUE 100.

ENDCLASS.                                                                    “CLASS DEFINITION

CLASS zcl_employer IMPLEMENTATION.

METHOD get_income.

income = me->income * 80.

ENDMETHOD.

ENDCLASS.                                                                   “CLASS IMPLEMENTATION

 

Now, I want to create a subclass of it, where I want to redefine this method ‘GET_INCOME’.

 

CLASS zcl_manager DEFINITION INHERITING FROM zcl_employer.

PUBLIC SECTION.

METHODS: get_income REDEFINITION.

PRIVATE SECTION.

DATA: income TYPE F VALUE 100.

ENDCLASS.                                                                    “CLASS DEFINITION

CLASS zcl_manager IMPLEMENTATION.

METHOD get_income.

income = me->income * 100.

ENDMETHOD.

ENDCLASS.                                                                   “CLASS IMPLEMENTATION

 

Once you derived a class from another class, the new class will be known as ‘SUBCLASS’ or ‘CHILD CLASS’. And the class from which a new class is deriving will be known as ‘SUPER CLASS’ or ‘PARENT CLASS’.

As discussed earlier, the subclass can have all the properties, & methods of a super class. The developer can redefine the existing methods of the super class and he/she can also add some new functionality into it.

Advantages:

Provides reusability of code functionality & fast implementation time. It can have all the advantages of ‘Inheritance’.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author