Professional Documents
Culture Documents
Object Oriented Abap
Object Oriented Abap
Global classes
This document may discuss sample coding or other information that does not include SAP official interfaces
and therefore is not supported by SAP. Changes made based on this information are not supported and can
be overwritten during an upgrade.
SAP will not be held liable for any damages caused by using or misusing the information, code or methods
suggested in this document, and anyone using these methods does so at his/her own risk.
SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of
this technical article or code sample, including any liability resulting from incompatibility between the content
within this document and the materials and services offered by SAP. You agree that you will not hold, or seek
to hold, SAP responsible or liable with respect to the content of this document.
Applies To:
SAP Web AS, SAP R/3 4.0 and above
Summary
The use of Object-Oriented (OO) ABAP is ever-increasing. The classical ABAP programming paradigm is
giving way to the Object Orientation. One important aspect of Object-Oriented ABAP is the portability. This
two-part document explains how the Classes and their components can be imported and exported in ABAP.
This also lists the limitations of the tool provided by SAP.
This document, Part I, explains how the Local Classes are imported into the Global Classes visible in the
ABAP Class Builder (Transaction SE24). It gives the details of the relationships between the local and global
class components, and the limitations of the import step. Part II will explain the other way around, that is,
exporting the Global Classes into the Local Classes.
Table of Contents
Applies To:........................................................................................................................................2
Summary ..........................................................................................................................................2
Introduction.......................................................................................................................................5
Requirements: ..................................................................................................................................5
Description ................................................................................................................................8
Instantiation ...............................................................................................................................8
Message Class..........................................................................................................................8
Category....................................................................................................................................8
Abstract ...................................................................................................................................10
Final.........................................................................................................................................10
Level........................................................................................................................................11
Visibility ...................................................................................................................................11
Modeled...................................................................................................................................11
Typing......................................................................................................................................11
Associated Type......................................................................................................................11
Description ..............................................................................................................................12
Level........................................................................................................................................12
Visibility ...................................................................................................................................12
Modeled...................................................................................................................................13
Description ..............................................................................................................................13
Abstract ...................................................................................................................................13
Final.........................................................................................................................................13
Type ........................................................................................................................................14
Visibility ...................................................................................................................................14
Modeled...................................................................................................................................14
Description ..............................................................................................................................14
Visibility ...................................................................................................................................15
Modeled...................................................................................................................................15
Typing......................................................................................................................................15
Associated Type......................................................................................................................15
Description ..............................................................................................................................15
Aliases ........................................................................................................................................16
Visibility ...................................................................................................................................16
Conclusion:.....................................................................................................................................16
Author Bio.......................................................................................................................................19
Introduction
The Object-Oriented ABAP is available in three flavors:
o The BOR is accessible via transaction SWO1. It was an early adaptation of Object-
Orientation by SAP. Its Object-Orientated programming is heavily based on Macros, and
does not offer several features of Object Orientation.
• Global Classes
o The Global Classes are accessible via ABAP Class Builder (Transaction SE24). It has a very
detailed GUI support for Object Orientation.
• Local Classes
o The Local Classes are ABAP programs, accessible via SE38 and related Transactions.
This document explains how the Local Classes are imported into the Global Classes using the tool provided
by SAP. This also lists:
• How each attribute or property in a global class is imported from its equivalent ABAP construct in the
local class.
• A sample local class consisting of several Object-Oriented ABAP constructs. It uses the Customer
and Business Partner objects. This should also help in learning the Object-Oriented ABAP language.
Requirements:
The Local Class should be active as an ABAP program available via transaction SE38. It should have at least
one valid class or interface definition.
• In the window that appears next, enter the ABAP program name that has the Local Class.
• Hit the Enter button or the Icon next to the program name. The class declaration(s) in the program will
appear in the table control, followed by the names proposed by SAP for the corresponding Global
Class.
If the Local Class Program has multiple Local Class and / or declarations, each of these will be proposed as a
separate Global Class and / or Global Interface.
• Select the proposed Global Classes and Interfaces that you wish to generate.
• The proposed names start with ‘CL_’ or ‘IF_’, which will not be in the customer namespace. Change
the names to appropriate names in the customer namespace(s).
• Checkbox ‘Overwrite Existing Classes’: If a Global Class with the name already exists, select this.
• Checkbox ‘Explode INCLUDEs’: If the Local Class program has INCLUDE programs, select this.
• When importing from a Local Class that has a Hierarchical dependency in the same program, it is
advised to import all the related classes in one import step. Otherwise, the super class names in the
generated global class will default to the name proposed by SAP and will not match the correct super
class name given in customer namespace. The same applies to Interface references.
The components in the generated Global Class very closely match those in the Local Class. However, there
are some limitations. Here are the details, explained for each Tab in the Class Builder screen for easy
understanding.
The class gets most of the properties from the local class. However, there are some limitations.
Description
The local class has no place for this. Consequently, the class name itself appears as the description.
Instantiation
• Means that no other class can inherit from this class. In our example, the syntax is CLASS
Z_LOCAL_CLASS1 DEFINITION INHERITING FROM Z_LOCAL_CLASS FINAL.
Modeled only
Message Class
Program Status
• Ignored
Category
• This is ignored. In the example, none of the Type Pools REBP1 and REBP2, the Class
CL_GUI_PICTURE, and the Interface IF_RECIPIENT_BCS appears in the global class, even
though these are declared in several sections in the local class.
‘Interfaces’ Tab
All the Interfaces declared in the local class with the INTERFACES statement in the public section are
imported. If these global interfaces include other interfaces, those also appear in the global class.
Abstract
• The addition ALL METHODS ABSTRACT in the local class makes the Interface as Abstract.
This is applicable only for Abstract Interfaces.
Final
• The addition ALL METHODS FINAL in the local class makes the Interface as Final. However,
it should have all of its methods as Instance Methods and none as Static.
‘Friends’ Tab
The import step seems to ignore the Friend classes in the Local Class. In our example, the class
CL_BUPA_BCS is declared as a Friend of Z_LOCAL_CLASS, but is not imported into the global
class.
‘Attributes’ Tab
The Attributes get most of the properties from the local class attributes. However, there are some
limitations, as follows. These are mainly due to the fact that the Report programs are allowed to have
some ABAP constructs that the ABAP Object are not allowed.
Level
Visibility
• Can be set to Public / Protected / Private, by including the declaration in the PUBLIC
SECTION / PROTECTED SECTION / PRIVATE SECTION of the local class
Modeled
Typing
• Can be set to Like / Type / Type Ref To by declaring with the LIKE / TYPE / TYPE REF TO
reference in the local class.
Associated Type
• Imported from the LIKE / TYPE / TYPE REF TO reference in the local class.
• Built-in declarations are not imported, such as built-in structures and table types. These are
shown with the Associated Type ‘*** invalid ***’. For example, G_SUB_AREA
• References in the local class to ABAP Data Types (such as CHAR, NUMC, and DEC etc) will
not be imported. These will be shown as ‘*** invalid ***’ after import.
Description
Initial value
‘Methods’ Tab
The Methods get most of the properties from the local class attributes. However, this also has some
limitations, similar to the Attributes, on the allowed local Data declarations in the methods.
Level
• Instance: By Default
Visibility
• Can be set to Public / Protected / Private, by including the declaration in METHODS the
PUBLIC SECTION / PROTECTED SECTION / PRIVATE SECTION of the local class
Modeled
Description
Abstract
• The addition ABSTRACT in the local class makes the Method Abstract. This is applicable
only for Abstract Classes.
Final
• The addition FINAL in the local class makes the Method Final. An example is the method
INCREMENT_COUNTER. Such Methods can not be re-implemented in the inherited classes.
Event Handler
• This can be set in the local class by <method> for event <event> of <class> [importing
parameters]. Event Handler Methods can only import parameters, and can not have
exporting, changing or returning parameters.
Editor Lock
‘Events’ Tab
Type
• Instance: By Default
Visibility
• Can be set to Public / Protected / Private, by including the EVENTS declaration in the
PUBLIC SECTION / PROTECTED SECTION / PRIVATE SECTION of the local class
Modeled
Description
Visibility
• Can be set to Public / Protected / Private, by including the TYPES declaration in the PUBLIC
SECTION / PROTECTED SECTION / PRIVATE SECTION of the local class
Modeled
Typing
• Can be set to Like / Type / Type Ref To by declaring with the LIKE / TYPE / TYPE REF TO
reference in the local class.
Associated Type
• Imported from the LIKE / TYPE / TYPE REF TO reference in the local class.
• Built-in declarations are not imported, such as built-in structures and table types. These are
shown with the Associated Type ‘*** invalid ***’. For example, T_SALES_AREA
• References in the local class to ABAP Data Types (such as CHAR, NUMC, and DEC etc) will
not be imported. These will be shown as ‘*** invalid ***’ after import.
Description
Aliases
Alias Name
• This is taken from the ALIASES declaration in the local class. An example is the
ALIAS_INITIALIZE Alias defined as ‘for IF_PARTNER_TRD~INITIALIZE’.
Visibility
• Can be set to Public / Protected / Private, by including the TYPES declaration in the PUBLIC
SECTION / PROTECTED SECTION / PRIVATE SECTION of the local class
• Documentation for Class, Attributes, Methods, Events, and Internal Types: Not imported
Conclusion:
The import tool provided by SAP to import local classes into global classes is very useful. This article
should help in understanding the import process, its limitations, and the relationships between the
local and global class elements.
report ZZ_LCL_LOCAL_CLASS_EXAMPLE.
*---------------------------------------------------------------------*
interface I1.
endinterface. "I1 DEFINITION
*---------------------------------------------------------------------*
* CLASS Z_LOCAL_CLASS DEFINITION
*---------------------------------------------------------------------*
class Z_LOCAL_CLASS definition create public friends CL_BUPA_BCS.
type-pools: REBP1,
REBP2.
public section.
ALIAS_ADDR_DATA_EXPORT for
IF_EX_BUPA_ADDR_EXPORT~ADDR_DATA_EXPORT.
protected section.
begin of T_SALES_AREA,
VKORG type VKORG,
VTWEG type VTWEG,
SPART type SPART,
end of T_SALES_AREA.
methods: PROTECTED_METHOD.
class-methods: STATIC_METHOD.
private section.
methods: PRIVATE_METHOD.
*---------------------------------------------------------------------*
* CLASS Z_LOCAL_CLASS1 DEFINITION
*---------------------------------------------------------------------*
class Z_LOCAL_CLASS1 definition inheriting from Z_LOCAL_CLASS final.
endclass. "Z_LOCAL_CLASS1 DEFINITION
*---------------------------------------------------------------------*
* CLASS Z_LOCAL_CLASS IMPLEMENTATION
*---------------------------------------------------------------------*
class Z_LOCAL_CLASS implementation.
define MACRO1.
&2 = &1 + 1.
end-of-definition.
method SET.
COUNT = SET_VALUE.
endmethod. "set
method INCREMENT_COUNTER.
data DIFF type I.
add 1 to COUNT.
if COUNT > THRESHOLD.
DIFF = COUNT - THRESHOLD.
raise event CRITICAL_VALUE exporting EXCESS = DIFF.
endif.
endmethod. "increment_counter
method GET.
GET_VALUE = COUNT.
endmethod. "get
method HANDLE_EXCESS.
write: / 'Excess is'(001), EXCESS.
endmethod. "handle_excess
method KUNNR_GET.
write: / 'This is an Example method'(002).
endmethod. "EXAMPLE
method KUNNR_EXISTS.
write: / 'This is a RETURNING method'.
endmethod. "RET
method PROTECTED_METHOD.
write: / 'This is a Protected method'.
endmethod. "PROTECTED_METHOD
method PRIVATE_METHOD.
write: / 'This is a Private method'.
endmethod. "PRIVATE_METHOD
method STATIC_METHOD.
write: / 'This is a Static method'.
endmethod. "STATIC_METHOD
Author Bio