Sie sind auf Seite 1von 19

Object Oriented ABAP - Local and

Global classes

SDN Community Contribution


(This is not an official SAP document.)

Disclaimer & Liability Notice

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.

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 1


Object Oriented ABAP - Local and
Global classes

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.

By: Bhanumurthy Thirumala

Company: Intelligroup, Inc.

Date: 05 January 2006

Table of Contents
Applies To:........................................................................................................................................2

Summary ..........................................................................................................................................2

Table of Contents .............................................................................................................................2

Introduction.......................................................................................................................................5

Requirements: ..................................................................................................................................5

The Local Class................................................................................................................................5

The IMPORT Step............................................................................................................................5

Import results and Limitations ..........................................................................................................7

Class Properties in ‘Properties’ Tab .............................................................................................7

Description ................................................................................................................................8

Instantiation ...............................................................................................................................8

Final / Super Class....................................................................................................................8

Modeled only .............................................................................................................................8

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 2


Object Oriented ABAP - Local and
Global classes

Message Class..........................................................................................................................8

Program Status .........................................................................................................................8

Category....................................................................................................................................8

Type Groups / Objects ..............................................................................................................9

Inheritance – example (Z_LOCAL_CLASS1) ...........................................................................9

‘Interfaces’ Tab .............................................................................................................................9

Abstract ...................................................................................................................................10

Final.........................................................................................................................................10

‘Friends’ Tab ...............................................................................................................................10

‘Attributes’ Tab ............................................................................................................................10

Level........................................................................................................................................11

Visibility ...................................................................................................................................11

Modeled...................................................................................................................................11

Typing......................................................................................................................................11

Associated Type......................................................................................................................11

Description ..............................................................................................................................12

Initial value ..............................................................................................................................12

‘Methods’ Tab .............................................................................................................................12

Level........................................................................................................................................12

Visibility ...................................................................................................................................12

Modeled...................................................................................................................................13

Description ..............................................................................................................................13

Method properties accessed from the ‘Details’ Icon: ..............................................................13

Abstract ...................................................................................................................................13

Final.........................................................................................................................................13

Event Handler .........................................................................................................................13

Editor Lock ..............................................................................................................................14

‘Events’ Tab ................................................................................................................................14

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 3


Object Oriented ABAP - Local and
Global classes

Type ........................................................................................................................................14

Visibility ...................................................................................................................................14

Modeled...................................................................................................................................14

Description ..............................................................................................................................14

‘Internal Types’ Tab ....................................................................................................................14

Visibility ...................................................................................................................................15

Modeled...................................................................................................................................15

Typing......................................................................................................................................15

Associated Type......................................................................................................................15

Description ..............................................................................................................................15

Aliases ........................................................................................................................................16

Alias Name ..............................................................................................................................16

Visibility ...................................................................................................................................16

Other Class properties affected..................................................................................................16

Conclusion:.....................................................................................................................................16

Code of the sample Local Class.....................................................................................................16

Author Bio.......................................................................................................................................19

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 4


Object Oriented ABAP - Local and
Global classes

Introduction
The Object-Oriented ABAP is available in three flavors:

• Business Object Repository (BOR)

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.

• Limitations of the import tool

• 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.

The Local Class


The Local Class is an ABAP program. This will be used as the source for importing into the Global class. To
demonstrate the import steps, a local class Z_LOCAL_CLASS is declared in the executable report program
ZZ_LCL_LOCAL_CLASS_EXAMPLE. This contains most of the OO constructs offered in ABAP. Its code is
given at the end of this article.

The IMPORT Step


In the Transaction SE24, use the following Menu path:

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 5


Object Oriented ABAP - Local and
Global classes

• 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.

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 6


Object Oriented ABAP - Local and
Global classes

• 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.

• Then hit the ‘Import” Icon at the bottom (F5).

Import results and Limitations

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.

Class Properties in ‘Properties’ Tab

The class gets most of the properties from the local class. However, there are some limitations.

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 7


Object Oriented ABAP - Local and
Global classes

Description

The local class has no place for this. Consequently, the class name itself appears as the description.

Instantiation

• Public by default, cannot be specified in the local class program.

• Protected / Private: Can be achieved by using the variant CLASS Z_LOCAL_CLASS


DEFINITION CREATE PROTECTED | PRIVATE.

Final / Super Class

• 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

• Not supported in Local classes

Message Class

• The local class has no place for this. It is left blank.

Program Status

• Ignored

Category

• Always General Object Type

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 8


Object Oriented ABAP - Local and
Global classes

Type Groups / Objects

• 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.

Inheritance – example (Z_LOCAL_CLASS1)

‘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.

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 9


Object Oriented ABAP - Local and
Global classes

The Interface properties are imported as follows:

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.

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 10


Object Oriented ABAP - Local and
Global classes

Level

• Instance: With DATA declaration

• Static: With CLASS-DATA declaration. For example, G_KUNNR_STATIC

• Constant: With CONSTANTS declaration. For example, G_BUKRS_CONSTANT

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

• Not available for import

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.

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 11


Object Oriented ABAP - Local and
Global classes

Description

• Not available for import

Initial value

• Imported from the VALUE property in the Declaration

‘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

• Static: With CLASS-METHODS declaration. For example, STATIC_METHOD

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

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 12


Object Oriented ABAP - Local and
Global classes

Modeled

• Not available for import

Description

• Not available for import

Method properties accessed from the ‘Details’ Icon:

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.

• An example is the method HANDLE_EXCESS, declared in the local class as


HANDLE_EXCESS for event CRITICAL_VALUE of Z_LOCAL_CLASS importing EXCESS.

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 13


Object Oriented ABAP - Local and
Global classes

Editor Lock

• Not available for import

‘Events’ Tab

Type

• Instance: By Default

• Static: With CLASS-EVENTS declaration. For example, STATIC_EVENT

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

• Not available for import

Description

• Not available for import

‘Internal Types’ Tab

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 14


Object Oriented ABAP - Local and
Global classes

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

• Not available for import

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

• Not available for import

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 15


Object Oriented ABAP - Local and
Global classes

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

Other Class properties affected

• Text Elements: Not imported

• Macros: Not imported

• 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.

Code of the sample Local Class

report ZZ_LCL_LOCAL_CLASS_EXAMPLE.

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 16


Object Oriented ABAP - Local and
Global classes

*---------------------------------------------------------------------*
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.

class CL_GUI_PICTURE definition load.


interface IF_RECIPIENT_BCS load.
type-pools: REBP1,
REBP2.

types: T_KUNNR type KUNNR.

class-data: G_KUNNR_STATIC type KUNNR.

data: COUNT type I,


THRESHOLD type I value 10,
G_READONLY type CHAR15 read-only.

constants: G_BUKRS_CONSTANT type BUKRS value '0001'.

events CRITICAL_VALUE exporting VALUE(EXCESS) type I.

methods: KUNNR_GET importing REFERENCE(IM_KUNNR) type KUNNR


VALUE(IM_BUKRS) type BUKRS
default G_BUKRS_CONSTANT
VALUE(IM_SORTL) type SORTL optional
exporting VALUE(EX_NAME1) type NAME1
changing VALUE(CH_SORTL) type SORTL
exceptions X_KUNNR_WRONG
X_INACTIVE,

KUNNR_EXISTS importing REFERENCE(IM_KUNNR) type KUNNR


returning VALUE(SUBRC) type SUBRC
raising CX_ABABUSINESS_PARTNER_FAULT,

HANDLE_EXCESS for event CRITICAL_VALUE of


Z_LOCAL_CLASS importing EXCESS,

SET importing VALUE(SET_VALUE) type I optional,

GET exporting VALUE(GET_VALUE) type I,

INCREMENT_COUNTER final. "Increment

interfaces: IF_EX_BUPA_STATUS all methods final,


IF_PARTNER_TRD,
IF_EX_BUPA_ADDR_EXPORT.

aliases: ALIAS_INITIALIZE for IF_PARTNER_TRD~INITIALIZE,

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 17


Object Oriented ABAP - Local and
Global classes

ALIAS_ADDR_DATA_EXPORT for
IF_EX_BUPA_ADDR_EXPORT~ADDR_DATA_EXPORT.

protected section.

class CL_GUI_PICTURE definition load.


interface IF_RECIPIENT_BCS load.
type-pools: REBP1,
REBP2.

types: T_VKORG_PROT type VKORG,

begin of T_SALES_AREA,
VKORG type VKORG,
VTWEG type VTWEG,
SPART type SPART,
end of T_SALES_AREA.

data: G_VKORG_PROT type T_VKORG_PROT.

methods: PROTECTED_METHOD.

class-methods: STATIC_METHOD.

aliases: ALIAS_GET_CHANGES for IF_PARTNER_TRD~GET_CHANGES.

private section.

types: T_SPART_PRIV type SPART.

data: G_SPART_PRIV type T_SPART_PRIV value '01'.

data: begin of G_SUB_AREA,


VTWEG type VTWEG,
SPART type SPART,
end of G_SUB_AREA.

methods: PRIVATE_METHOD.

class-events: STATIC_EVENT exporting VALUE(COUNTER) type I.

endclass. " Z_LOCAL_CLASS DEFINITION

*---------------------------------------------------------------------*
* 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.

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 18


Object Oriented ABAP - Local and
Global classes

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

endclass. " Z_LOCAL_CLASS IMPLEMENTATION

Author Bio

Bhanumurthy Thirumala is a SAP Technical Consultant with 7 years of SAP experience,


currently working with Intelligroup as Principal Consultant. He is also a SAP certified XI consultant.

© 2005 SAP AG The SAP Developer Network: http://sdn.sap.com 19

Das könnte Ihnen auch gefallen