You are on page 1of 24

1 / 24 04-09-2003 4:46 PM

)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi


A Ab ba ap p O Ob bj je ec ct ts s
A AB BA AP P O Ob bj je ec ct ts s

TabIe of contents
1. Defining Classes and Interfaces 2
1.1. Overview ___________________________________________________________________2
1.2. Defining Classes _____________________________________________________________2
1.2.1. Declaration Section2
1.2.1.1. Visibility Areas 3
1.2.1.2. Class Components 3
1.2.1.3. Global Classes 4
1.2.1.4. DeIining Subclasses4
1.2.1.5. Abstract Classes5
1.2.1.6. Final Classes 5
1.2.1.7. Instantiation 6
1.2.1.8. Friends 6
Local Iriends oI global classes 7
1.2.2. Implementation Section 8
1.2.3. Declaring Classes 9
1.3. Defining Interfaces __________________________________________________________10
1.3.1. Declaration Section10
1.3.1.1. InterIace Components 10
1.3.2. Declaring InterIaces 11
1.4. Declaring Components in Classes and Interfaces _________________________________11
1.4.1. Methods 12
1.4.1.1. Instance Methods12
1.4.1.2. General Instance Methods 12
1.4.1.3. Functional Instance Methods 16
1.4.1.4. Instance Constructors18
1.4.1.5. Event Handlers19
1.4.1.6. RedeIining Instance Methods 20
1.4.1.7. Static Methods 21
1.4.1.8. General Static Methods 22
1.4.1.9. Functional Static Methods 22
1.4.1.10. Static Constructors23
1.4.1.11. Static Event Handlers24






















2 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s

1. Defining Classes and Interfaces
1.1. Overview
This chapter describes the definition of classes and interfaces, as well as their components.
Classes and interfaces are the foundation of ABAP Objects, the object-oriented component of the ABAP
language. You can define classes and interfaces in ABAP programs with the following program types:

v n a class pool ,you can use the Class Builder in the ABAP Workbench to define one global class
of the class library. This global class can e used in all other ABAP programs. You can also define
local classes and inter- faces in a class pool, for use in the class pool itself.
v n an interface pool ,you can use the Class Builder in the ABAP Work- bench to define one global
interface of the class library .This global interface can be used in all other ABAP programs. You
cannot define local classes or interfaces in an interface pool.
v n all other ABAP programs (except type groups),you can define local classes and interfaces for
use in the program itself.

n addition to the declarations described in Chapter 6,you can declare additional components
within classes and interfaces. These components are also described in this chapter.

The statements for declaring classes and interfaces are:

Statement Section
CLASS 7.2
NTERFACE 7.3
METHODS, CLASS-METHODS 7.4 .1
EVENTS, CLASS-EVENTS 7.4 .2
NTERFACES ALASES 7.4 .3
1.2. Defining Classes
The full definition of a class consists of a declaration section and an implementation section, both
of which you introduce with CLASS You define the properties of a class and declare its components in the
declaration section. You implement the methods of the class in the implementation section. You use other
variants of CLASS to declare classes in a program.

1.2.1. DecIaration Section
Defines a class and declares its components.

Syntax

CLASS class DEFINITION [PUBLIC]
[INHERITING FROM superclass]
[ ABSTRACT]
[ FINAL]
[CREATE {PUBLIC|PROTECTED|PRIVATE}]
[[ GLOBAL] FRIENDS [classi] [ifaci]].
[PUBLIC SECTION].
[components]
[PROTECTED SECTION].
[components]
[PRIVATE SECTION].
[components]
ENDCLASS.





3 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s

The statement block CLASS class DEFINITION ENDCLASS defines a class. You declare
components of the class,components between CLASS and ENDCLASS You have to specify each
component after one of the statements PUBLIC SECTION PROTECTED SECTION, or PRIVATE
SECTION and you have to list those in the specified order. A class does not have to contain all the
SECTION statements.

You can use the additions of the CLASS statement to publish a class globally in the class library,
define an inheritance relationship, make the class abstract or final, define whether it can e instantiated,
and offer friendship to other classes and/or interfaces.



1.2.1.1. VisibiIity Areas

PubIic visibiIity area

Syntax

PUBLIC SECTION.

This statement defines the public visibility area of class class All class components that you
declare in the area after the PUBLIC SECTION statement can be addressed from outside the class, from
within its sub- classes, and within the class itself.

Protected visibiIity area

Syntax

PROTECTED SECTION.

This statement defines the protected visibility area of class class. All class components that you
declare in the area after the PROTECTED SECTION statement can be addressed in the subclasses of the
class and within the class itself.


Private visibiIity area

Syntax

PRIVATE SECTION.

This statement defines the private visibility area of class class. All class components that you
declare in the area after the PRIVATE SECTION statement can be addressed only within the class itself.


1.2.1.2. CIass Components

Syntax

components.

You define the components of the classes in the visibility areas. The following declaration
statements are possible for components.






4 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
v TYPE-POOLS TYPES DATA CLASS-DATA and CONSTANTS for data types and data objects
(see Chapter 6).
v METHODS CLASS-METHODS EVENTS and CLASS-EVENTS for methods and events (see
Section 7.4).
v INTERFACES for implementing interfaces and ALIASES for alias names of interface
components (see Section 7.4).

Note
All the components of a class lie within one namespace.The name of a component must be
unique within a class, regardless of its type (data type, attribute, method, event, or alias name).The
components of an implemented interface differ from the directly declared components of the class
through prefix ifac~(name of the interface with the interface component selector).



1.2.1.3. GIobaI CIasses

Syntax

... PUBLIC ...

When you use the addition PUBLIC the class becomes a global class of the class library. The
addition PUBLIC is possible for only one class of a class pool and is generated y the Class Builder when
you create a global class. All classes without the addition PUBLIC are local classes of their program.

Note
You cannot use the TYPES statement to declare separate data types in the public visibility area
of a global class. Moreover, you can use only globally declared data types there.



1.2.1.4. Defining SubcIasses

Syntax

... INHERITING FROM supercIass ...

f you use the addition INHERITING FROM the class is derived from the superclass
superclass through inheritance, and therefore becomes its direct subclass .The superclass
superclass can be any non-final class that is visible at this point.

Each class can have only one direct superclass, but can have several direct subclasses (simple
inheritance ).Any class without the addition INHERITING FROM implicitly inherits from the predefined
empty class object. Taken together, all the classes in ABAP Objects form an inheritance tree, in which a
clearly defined path exists from each class to the root node, object.

The class class inherits the components of its superclass superclass without changing their
visibility area. Only the components of the public and protected visibility areas of the superclass are
visible in the subclass. The attributes of the inherited components cannot be changed. You can also
declare additional components in a subclass and redefine inherited methods that is, you can re-
implement them without changing the interface.






5 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
Note
The public and protected components of all the classes within a path of the inheritance tree all lie
within the same namespace. New components in a subclass may not have names that are identical to
public or protected components that were inherited from the superclasses.


1.2.1.5. Abstract CIasses

Syntax

... ABSTRACT ...

The addition ABSTRACT defines an abstract class, class. You cannot generate instances of an abstract
class. To use the instance components of an abstract class, you have to instantiate a non-abstract
subclass of the class.


1.2.1.6. FinaI CIasses

Syntax

... FINAL ...

The addition FINAL defines a final class class. You cannot derive subclasses of a final class. All
methods of a final class are implicitly final, which means that you cannot explicitly declare them as final.

Note
f a class is both abstract and final, you can use only its static components. While you can declare
instance components, you cannot use them.

ExampIe
n the example below, an abstract class c1 and a final class c2 are defined, and c2 inherits from c1
c1 is implicitly a subclass of the blank class object You can access m1 in c2 but a1.

CLASS c1 DEFINITION ABSTRACT.
PROTECTED SECTION.
METHODS m1.
PRIVATE SECTION.
DATA a1 TYPE string VALUE `Attribute A1 of class C1`.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1 FINAL.
PUBLIC SECTION.
METHODS m2.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
METHOD m1.
WRITE / a1.
ENDMETHOD.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
METHOD m2.
m1( ).
ENDMETHOD.





6 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
ENDCLASS.
...
DATA oref TYPE REF TO c2.
CREATE OBJECT oref.
oref->m2( ).



1.2.1.7. Instantiation

Syntax

... CREATE {PUBLIC|PROTECTED|PRIVATE} ...

The addition CREATE defines the context in which class can be instantiated that is, where
the CREATE OBJECT statement can be executed for that class.

v A class with the addition CREATE PUBLIC can be instantiated wherever that class is visible.
v A class with the addition CREATE PROTECTED can be instantiated only within methods of its
subclasses and within the class itself.
v A class with the addition CREATE PRIVATE can e instantiated only within methods the class itself.
This means that you cannot instantiate them as inherited components of subclasses, either.
Whether or not a class can be instantiated also depends on its direct superclass :

v Direct subclasses of object or a class with addition CREATE PUBLIC implicitly inherit the
addition CREATE PUBLIC You can explicitly specify any CREATE addition to override the
inherited one.
v Direct subclasses a class with addition CREATE PROTECTED implicitly inherit the addition
CREATE PROTECTED You can explicitly specify any CREATE addition to override the inherited
one.
v Direct subclasses of a class with addition CREATE PRIVATE that are not friends of that class are
explicitly assigned the addition CREATE NONE. You cannot instantiate these classes, nor can you
specify any explicit CREATE additions. Direct subclasses that are friends of the class implicitly
inherit the addition CREATE PRIVATE You can explicitly specify all CREATE additions for friends
of superclasses that can e instantiated as private to override the inherited one.

Note
f a class can be instantiated as private, we recommend making it final as well, as its subclasses
cannot e instantiated if they are not friends of the class.



1.2.1.8. Friends

Syntax

... [GLOBAL] FRIENDS [cIassi] [ifaci].

The addition FRIENDS makes class class a friend of classes classi and/ or interfaces ifaci
At the same time, all subclasses of classes classi all classes that implement one of the interfaces ifaci
and all interfaces that have one of the interfaces ifaci as a component interface become friends of
class You have to specify at least one class or interface.

The friends of a class have unrestricted access to the protected and private components of that
class, and can generate unrestricted instances of the class.





7 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s

The friends of class are not automatically friends of subclasses of class. Moreover, the
addition FRIENDS does not make class a friend of its friends.

f you do not use the GLOBAL additions, you can specify all classes and interfaces for classi
and ifaci that are visible at this point. f you define global classes and interfaces in the class library as
friends, please note that in these objects the local classes of other ABAP programs are not visible. n
such cases, static access to the components of class is not possible.

The addition GLOBAL is allowed only when you use the addition PUBLICfor the global class of a
class pool at the same time. You can list other. global classes and interfaces from the class library after
GLOBAL FRIENDS

This addition is generated when the Class Builder creates a global class and you have specified
friends at the corresponding tab page of the Class Builder.

ExampIe
n the example below, class c2 is a friend of interface i1 and therefore also of the implementing class
c1 t can instantiate these objects and access their private component a1.

INTERFACE i1.
...
ENDINTERFACE.

CLASS c1 DEFINITION CREATE PRIVATE FRIENDS i1.
PRIVATE SECTION.
DATA a1(10) TYPE c VALUE 'Class 1'.
ENDCLASS.

CLASS c2 DEFINITION.
PUBLIC SECTION.
INTERFACES i1.
METHODS m2.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
METHOD m2.
DATA oref TYPE REF TO c1.
CREATE OBJECT oref.
WRITE oref->a1.
ENDMETHOD.
ENDCLASS.


LocaI friends of gIobaI cIasses

A separate statement exists for this situation.

Syntax

CLASS cIass DEFINITION LOCAL FRIENDS [cIassi] [ifaci].

This statement turns the local classes and interfaces of a class pool, classi and ifaci into
friends of its global class. You have to specify at least one class or interface.






8 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
This section does not introduce a declaration section, and you cannot complete it with ENDCLASS
You have to specify it in the class pool before the definition of the local classes and interfaces, classi
and ifaci if you want them to have access to the private and protected components of the global class,
class.



1.2.2. ImpIementation Section

mplements the methods of a class.

Syntax

CLASS class IMPLEMENTATION.
...
METHOD ...
...
ENDMETHOD.
...
ENDCLASS.

n the statement block CLASS class MPLEMENTATON ENDCLASS you have to implement the
following methods of a class (in any sequence):

v All non-abstract methods that are declared in the declaration section of the class with
METHODS or CLASS-METHODS.
v All non-abstract methods of interfaces that are declared in the declara- tion section of the
class with the NTERFACES statement.
v All the methods inherited from superclasses that are listed in the decla ration segment of the
class with the statement METHODS ... REDEFNTON.

The implementation of each method corresponds to a METHOD END-METHOD processing
block (see Section 4.2.1).No statements are allowed outside of method implementations in the
implementation section. Within a method implementation, you can access all the components in instance
methods and all static components of their own class in static methods.The component selector is not
necessary to address compo nents in their own class. An implicitly generated local reference variable
called me is available in the implementation of every instance method .me points to the current instance
of the method.

Note
A class that in accordance with its declaration section does not have to implement any
methods has either a blank implementation section or none at all.

ExampIe
n the following example, three methods of class c2 have to be implemented. Method m1 in c1
is abstract and does not have to be implemented.

INTERFACE i1.
METHODS m1.
ENDINTERFACE.

CLASS c1 DEFINITION ABSTRACT.
PROTECTED SECTION.
METHODS m1 ABSTRACT.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.





9 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
INTERFACES i1.
METHODS m2.

PROTECTED SECTION.
METHODS m1 REDEFINITION.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
METHOD m1.
...
ENDMETHOD.

METHOD m2.
...
ENDMETHOD.

METHOD i1~m1.
...
ENDMETHOD.

ENDCLASS.


1.2.3. DecIaring CIasses

n some cases, you have to explicitly declare the definition of a class before you can use it.

Syntax

CLASS class DEFINITION { DEFERRED | LOAD }.

The two variants of the CLASS statement serve to declare a class class in the program
independently of the location of the actual definition. They do not introduce a declaration section, and you
cannot complete them with ENDCLASS. The additions have the following meanings:

v The variant with addition DEFERRED declares a local class before its actual definition in
the program. The program has to have a declaration section for class later in the
program. This statement is required if you want to refer to a class before it is actually
defined. Access to individual components is not possible before the actual definition.
v The variant with addition LOAD loads a global class from the class library. This
statement is required prior to Release 6.40 if you want to access one of the static
components of class within a program, or if you want to declare an event handler for
class before class is loaded automatically. All other accesses load class
automatically. n Release 6.40 and later, the addition LOAD is required only when the
compiling of an ABAP program fails because it contains recursive accesses to a global
class. n such cases, you may be a le to make the program compliable by explicitly
loading the class before the recursion.

ExampIe
n the following example, class c1 uses class c2 and vice-versa. As a result, one of the classes
has to e declared before its actual definition. n addition, global class cl_gui_cfw is loaded explicitly
before one of its static attributes is used.

CLASS c1 DEFINITION DEFERRED.
CLASS c2 DEFINITION.
PUBLIC SECTION.
DATA c1ref TYPE REF TO c1.
ENDCLASS.





10 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
CLASS c1 DEFINITION.
PUBLIC SECTION.
DATA c2ref TYPE REF TO c2.
ENDCLASS.
CLASS cl_gui_cfw DEFINITION LOAD.
DATA state LIKE cl_gui_cfw=>system_state.


1.3. Defining Interfaces

NTERFACE

The definition of an interface consists of a declaration section that is introduced with INTERFACE
n contrast to classes, interfaces do not have an implementation section. You use other variants of
INTERFACE to declare interfaces in a program.

1.3.1. DecIaration Section

Defines an interface and declares its components.

Syntax

INTERFACE ifac.
[components]
ENDINTERFACE.

The statement block INTERFACE ENDINTERFACE defines an interface ifac. You declare
the components of the interface between INTERFACE and ENDINTERFACE.



1.3.1.1. Interface Components

Syntax

components

The following declaration statements are possible for components
v TYPE-POOLS TYPES DATA CLASS-DATA and CONSTANTS for data types and data
objects (see Chapter 6).
v METHODS CLASS-METHODS EVENTS and CLASS-EVENTS for methods and events (see
Section 7.4).
v INTERFACES for integrating component interfaces and ALIASES for alias names of
their components (see Section 7.4).

Note
All the components of an interface lie within one namespace. The name of a component must e
unique within an interface, regardless of its type (data type, attribute, method, event,or alias name).The
components of an integrated interface differ from the directly declared components through prefix
ifac~(name of the interface with the interface component selector).






11 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
ExampIe
n the following example, an interface, i1 is declared with three interface components, a1, m1 and
e1. Class c1 implements the interface, which makes the interface components public components of the class
that can be addressed using the interface component selector (~).

INTERFACE i1.
DATA a1 TYPE string.
METHODS m1.
EVENTS e1 EXPORTING value(p1) TYPE string.
ENDINTERFACE.
CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES i1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD i1~m1.
RAISE EVENT i1~e1 EXPORTING p1 = i1~a1.
ENDMETHOD.
ENDCLASS.




1.3.2. DecIaring Interfaces
n some cases, you have to explicitly declare the definition of an interface before you can use it.

Syntax

INTERFACE ifac { DEFERRED | LOAD }.

The two variants of the INTERFACE statement serve to declare interface ifac independently of
the location of the actual definition in the program. They do not introduce a declaration section, and you
cannot complete them with ENDINTERFACE. The meanings of the two statements for interfaces are
identical to the meanings of the CLASS statements described for classes in Section 7.2.3.


1.4. Declaring Components in Classes and Interfaces
You use the declaration section of classes and interfaces to declare the components of classes and
interface that are major components of a class:

v Attributes
v Methods
v Events

Attributes are the data objects of a class. You can also declare class-specific data types. You declare
attributes and data types with the general statements TYPE-POOLS TYPES DATA CLASS-DATA and
CONSTANTS, which with the exception of CLASS-DATAcan also be used in other contexts and are
described in Chapter 6.

To declare methods and events, you use specific statements that are possible only in the declaration
section of classes and interfaces.









12 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
1.4.1. Methods

You use the METHODS and CLASS-METHODS statements to declare methods. They define how
the class responds. You define the interface of a method in the declaration. Different categories of
methods exist for specific tasks:

v General methods
v Functional methods
v Constructors
v Event handlers

Any of these methods can be declared as an instance method or a static method.



1.4.1.1. Instance Methods

METHODS

The METHODS statement declares instance methods. nstance methods are tied to objects. To
use instance methods, you first have to generate an object of the class. n instance methods, you can
access all components of the method 's own class without requiring a component selector .



1.4.1.2. GeneraI Instance Methods

The most general form of the METHODS statement lets you define instance
methods with any input and output parameters .

Syntax

METHODS meth [ABSTRACT|FINAL]
[IMPORTING {parameteri} [PREFERRED PARAMETER p]]
[EXPORTING {parameteri}]
[CHANGING {parameteri}]
[{RAISING|EXCEPTIONS} {exci}].

This statement declares a general instance method, meth. The additions ABSTRACT and
FINAL make the method abstract and final, respectively. The other additions define the parameter
interface of the method and define which exceptions the method can propagate and/or trigger.


Parameter interface

The additions IMPORTING EXPORTING and CHANGING define the signature of method meth:
v IMPORTING defines input parameters
When you call the method, you have to specify a suitable actual parameter for each non-
optional input parameter. The content of the actual parameter is passed on to the input
parameter during the call.
v EXPORTING defines output parameters
When you call the method, you can specify a suitable actual parameter for each output
parameter. The content of the output parameter is passed on to the actual parameter
when the method ends free of errors.
v CHANGING defines input/output parameters





13 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
When you call the method, you have to specify a suitable actual parameter for each non-
optional input/output parameter. The content of the actual parameter is passed on to the
input/output parameter during the call, and the content of the input/parameter is passed
on to the actual parameter when the method ends.

You define the corresponding formal parameters by specify a list, para-material after each
addition.

Syntax of parameteri

... { {VALUE(pi)}|{REFERENCE(pi)}|{pi} {typing}
[OPTIONAL|{DEFAULT defi}] } ...

VALUE or REFERENCE defines whether a parameter, pi is passed on as a value or a reference.
The parameter is passed on as a reference y default only when you specify a name pi An input parameter
that is passed on as a reference cannot e changed in the method. You have to use the addition typing
to type each formal parameter. The syntax of typing is described in Section 8.2.When you type a
formal parameter, that typing is checked against the data type of any actual parameter that is passed on
(see Section 8.3).The defined type also determines the operand positions where the formal
parameter(s)can be used in the method.
You can us e OPTIONAL or DEFAULT to define input parameters and input/ output parameters
as optional parameters;if you use DEFAULT you can also specify a default parameter defi f a parameter
is optional,you do not have to specify an actual parameter when you call the method.f you use the
addition OPTIONAL the formal parameter is initialized type-specifically;if you use DEFAULT it assumes the
value of the default parameter,defi. defi can be any data object with a suitable type that is visible at this
point.

You can use PREFERRED PARAMETER to flag an input parameter p in list parameteri (after
IMPORTING as the preferred parameter. This addition makes sense, however, only if all the input
parameters are optional. f you call the method with syntax.

[CALL METHOD] meth( a ).

then the actual parameter a is assigned to preferred parameter p.


Note
f a formal parameter is typed as a reference variable and cannot e changed in the procedure, the
type check is performed as it is for a narrowing cast .f it can e changed in the procedure, the actual
parameter must be compatible with the formal parameter.

ExampIe
Method read_spfli_into_table in the following example has one input parameter and one
output parameter, both of which are completely typed through references to the ABAP Dictionary.

CLASS flights DEFINITION.
PUBLIC SECTION.
METHODS read_spfli_into_table
IMPORTING VALUE(id) TYPE spfli-carrid
EXPORTING flight_tab TYPE spfli_tab.
...
ENDCLASS.









14 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
CIass-based exceptions

Syntax

... RAISING {exci} ...

You can use the addition RAISING to pass class-based exceptions, exci on to the calling
program. The exceptions can be subclasses of CX_STATC_CHECK and CX_DYNAMC_CHECK which
are triggered in or propagated into the method of the ABAP runtime environment or with the RAISE
EXCEPTION statement, but are not handled in a TRY block (see Section 15.2).

exci can equal any exception classes that are subclasses of the above superclasses and are
visible at this point. You have to enter the exception classes in the ascending order of their inheritance
hierarchy.

f an exception of a superclass occurs in the method and is neither handled nor passed on, this
will cause either a syntax error or an exception that the calling program has to handle,
CX_SY_NO_HANDLER.

Notes
v Exceptions that involve the subclasses of CX_STATC_CHECK and
CX_DYNAMC_CHECK have to either be handled within the method or passed on
explicitly with the addition RAISING This is checked during the syntax check for
CX_STATC_CHECK, and at runtime for CX_DYNAMC_CHECK.

v You cannot use the CATCH SYSTEM-EXCEPTIONS statement in a method that
propagates class-based exceptions with addition RAISING You have to trap these
exceptions in a TRY block instead (see Chapter 15).

ExampIe
n class math below, method divide_1_by propagates all the exceptions represented by
class CX_SY_ARTHMETC_ERROR and its subclasses. f an input parameter, operand is given a 0
value during a call, for example, exception CX_SY_ZERODVDE is triggered, propagated, and as
illustrated in the example handled by the calling program in a TRY block.

CLASS math DEFINITION.
PUBLIC SECTION.
METHODS divide_1_by
IMPORTING operand TYPE I
EXPORTING result TYPE f
RAISING cx_sy_arithmetic_error.
ENDCLASS.

CLASS math IMPLEMENTATION.
METHOD divide_1_by.
result = 1 / operand.
ENDMETHOD.
ENDCLASS.
...
DATA oref TYPE REF TO math.
DATA exc TYPE REF TO cx_sy_arithmetic_error.
DATA result TYPE f.
DATA text TYPE string.

CREATE OBJECT oref.
TRY.





15 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
oref->divide_1_by( EXPORTING operand = ...
IMPORTING result = result ).
text = result.
CATCH cx_sy_arithmetic_error INTO exc.
text = exc->get_text( ).
ENDTRY.
MESSAGE text TYPE 'I'.


Exceptions prior to ReIease 6.10

Syntax

... EXCEPTIONS {exci} ...

You use the addition EXCEPTIONS to define a list of exceptions, exci that can be triggered in the
method with the RAISE or MESSAGE RAISING statements. You can specify any identifier exci for the
defined exceptions; you must specify them directly. Exceptions defined in this manner are tied to the
methods similar to formal parameters and cannot be propagated.

f an exception of this type is triggered in a method and not handled with the addition
EXCEPTIONS in the CALL METHOD statement during the method call, a runtime error occurs.


Note
You cannot use the additions RAISING and EXCEPTIONS at the same time.We recommend
using class-ased exceptions for new develop- ments in Release 6.10 and later,as they are independent of
any specific method.

ExampIe
n class math below, a separate exception arith_error is defined for method divide_1_by.
This exception is triggered with the RAISE statement when an arithmetic error occurs. For example, if an
input parameter, operand is given a 0 value during a call, exception arith_error is triggered during
the internal-method handling of exception CX_SY_ZERODVDE and is handled during the method call.

CLASS math DEFINITION.
PUBLIC SECTION.
METHODS divide_1_by
IMPORTING operand TYPE I
EXPORTING result TYPE f
EXCEPTIONS arith_error.
ENDCLASS.

CLASS math IMPLEMENTATION.
METHOD divide_1_by.
TRY.
result = 1 / operand.
CATCH cx_sy_arithmetic_error.
RAISE arith_error.
ENDTRY.
ENDMETHOD.
ENDCLASS.

...

DATA result TYPE f.





16 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
DATA text TYPE string.
DATA oref TYPE REF TO math.
DATA exc TYPE REF TO cx_sy_arithmetic_error.

CREATE OBJECT oref.

oref->divide_1_by( EXPORTING operand = ...
IMPORTING result = result
EXCEPTIONS arith_error = 4 ).

IF sy-subrc = 0.
WRITE result.
ELSE.
WRITE 'Arithmetic error!'.
ENDIF.



Abstract methods

Syntax

... ABSTRACT ...

The addition ABSTRACT is possible only in classes, not in interfaces. The addition ABSTRACT
defines an abstract method ,meth An abstract method is not implemented in the implementation section
of its class, and can e declared only in abstract classes. To implement an abstract method, you have to
redefine it in a non-abstract subclass with the addition REDEFINITION.

Note
Although abstract methods can be defined in classes that are both abstract and final, they cannot
be implemented, which means you cannot use them.



FinaI methods

Syntax

METHODS ... FINAL ...

The addition FINAL is possible only in classes, not in interfaces. The addition FINAL defines a
final method, meth A final method cannot be redefined in a subclass. All methods in final classes are
automatically final, and the addition FINAL is not allowed here.



1.4.1.3. FunctionaI Instance Methods

Functional methods can have any number of input parameters, and have exactly one return value
as output parameter.

Syntax

METHODS meth [ABSTRACT|FINAL]
[IMPORTING {parameteri}]





17 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
RETURNING VALUE(r) {TYPE type_spec}|{LIKE dobj_spec}
[{RAISING|EXCEPTIONS} {exci}].

This statement declares a functional instance method, meth. The additions ABSTRACT FINAL
IMPORTING RAISING and EXCEPTIONS have the same functions as for general instance methods.

nstead of the additions EXPORTING and CHANGING a functional method has one addition,
RETURNING that defines exactly one formal parameter, r as a return value .The return value must be
passed on with VALUE As far as the typing of return value r with typing is concerned, the same rules
apply as for the typing of IMPORTING, EXPORTING and CHANGING parameters (see Section 8.2),with
the exception that a return value always has to be completely typed: You cannot specify any of the
generic types from Table 5.3 for typing.

You can use functional methods in the operand positions of statements where a data object is
expected, as described in Section 2.2.5.The complete typing of the return value defines the data type of
the operand. When you execute statements of this type, the functional method is called, and the content
of the return value replaces the content of the operand.

Notes
v When you use a functional method in an operand position, class-based exceptions that
the method propagates with RAISING can be handled in a TRY block or propagated
further, as with the general methods. n contrast, the exceptions for a functional method
that you define with EXCEPTIONS cannot be handled in operand positions, and will
always cause a runtime error.

v f a functional method has the same name as a built-in function (see Section 5.4), the
expression meth( a ) in an operand position always calls the functional method. This
case can occur only in methods of the class in the functional method, however.


ExampIe

Functional method factorial in the following example has a return value, fact with type i.
The COMPUTE statement illustrates the method call in an operand position.

CLASS math DEFINITION.
PUBLIC SECTION.
METHODS factorial
IMPORTING n TYPE i
RETURNING value(fact) TYPE i.
ENDCLASS.

CLASS math IMPLEMENTATION.
METHOD factorial.
fact = 1.
IF n = 0.
return.
ELSE.
DO n TIMES.
fact = fact * sy-index.
ENDDO.
ENDIF.
ENDMETHOD.
ENDCLASS.
...
DATA oref TYPE REF TO math.





18 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
DATA result TYPE i.
CREATE OBJECT oref.
COMPUTE result = oref->factorial( ... ).


1.4.1.4. Instance Constructors
nstance constructors are methods with the predefined name constructor that are called
automatically during the instantiation of their class. Constructors can have any number of input
parameters, but no output parameters.

Syntax

METHODS constructor [FINAL]
[IMPORTING {parameteri}]
[{RAISING|EXCEPTIONS} {exci}].

This statement is possible only in the public visibility area of the declaration section of a class. t
declares the instance constructor of the class, constructor

Each class has a predefined method called constructor in its public visibility area. You can use
the explicit declaration to define the interface of method constructor class-specifically and to implement its
functionality. f you do not declare it explicitly, the instance constructor assumes the signature from the instance
constructor in the direct superclass and calls it implicitly.
f you implement the instance constructor in a subclass, you have to call the instance constructor of the
superclass explicitly (with the pseudo reference super->constructor and supply its interface with data.
The only exceptions to this are the direct subclasses of the root node, object Prior to the call of the superclass
constructor; an instance constructor can access only the static components of its class. After the superclass
constructor is called, it can also access the instance constructors.
You can use the addition IMPORTING to define input parameters, according to the same rules as for
the general methods. The additions RAISING and EXCEPTIONS for propagating and defining exceptions
also have the same meaning as under the general methods.
The instance constructor is called once for each instance of a class, with the CREATE OBJECT
statement, directly after the instance is generated. n the process, actual parameters must e assigned for all
non-optional input parameters, and exceptions can e handled or passed on. A call using CALL METHOD is not
possible, except in the call of the superclass constructor with super->constructor in the redefined
constructor of a subclass.

nstance constructors are implicitly final .You can specify the addition FINAL but it is not necessary.

Notes

v nstance constructors represent an exception to the rule that all public components along
a path in the inheritance tree have to line in the same namespace. The instance
constructor of each class has a separate interface and a separate implementation. You
cannot redefine an instance constructor.
v nstance constructors are declared only in the public visibility area of a class for technical
reasons. You control the actual visibility with the addition CREATE
{PUBLIC|PROTECTED|PRIVATE}of the CLASS DEFINITION statement.

ExampIe
n the following example, class c2 inherits from class c1. The instance constructor,
constructor is declared explicitly in both classes. t therefore has to be implemented in both classes. n
addition, the implementation in c2 has to contain the call of the superclass constructor.

CLASS c1 DEFINITION.





19 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
PUBLIC SECTION.
METHODS constructor IMPORTING p1 TYPE any.
...
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.
METHODS constructor IMPORTING p2 TYPE any.
...
ENDCLASS.

CLASS c1 IMPLEMENTATION.
METHOD constructor.
...
ENDMETHOD.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
METHOD constructor.
...
super->constructor( p2 ).
...
ENDMETHOD.
ENDCLASS.



1.4.1.5. Event HandIers

Event handlers are methods that can also e called with CALL METHOD but which are called
primarily y triggering an event of a class or an interface. The only possible formal parameters of an event
handler are the input parameters that are defined as the output parameters of the event.

Syntax

METHODS meth [ABSTRACT|FINAL]
FOR EVENT evt OF {class|ifac}
[IMPORTING {pi} [sender]].

This statement declares the instance method meth as the event handler evt of class class or
interface ifac class. ifac can be any classes or interfaces that are visible at this point and that
contain a visible event evt as a component.

f event evt is an instance event, event handler meth can handle it for all objects whose class
is class or a subclass of class or that implement interface ifac directly or through a superclass. f
the event is a static event, event handler meth can handle it for class and its subclasses, or for all
classes that implement interface ifac.

You can use the additions ABSTRACT and FINAL to make the event handler abstract or final,
just like the general methods.

The addition IMPORTING defines the input parameters of the event handler. pi can only be
names of formal parameters that you defined as export parameters of the event with addition EXPORTING
for statement [CLASS-]EVENTS in the declaration of event evt in the class or inter- face ifac The
additions TYPE LIKE and OPTIONAL DEFAULT are not possible. Any default parameters, the typing of





20 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
the input parameters, and whether or not the input parameters are optional are taken from the declaration
of the event. You do not have to specify all the output parameters of the event.

f evt is an instance event, you can also define a formal parameter called sender as an input
parameter of the event handler, in addition to its explicitly defined output parameters. sender is an
implicit output parameter of every instance event. t is fully typed as a reference variable that has class
or interface ifac as a static type, as defined in the declaration of the event handler after EVENT evt
OF f the event handler is called by an instance event, sender passes on a reference to the triggering
object.

Prior to Release 6.10,the static type of formal parameter sender was determined y the class or
interface in which the EVENTS statement declared the event. n Release 6.10 and later, the event handler
determines the type of its formal parameter itself.

ExampIe
n the example below, class picture contains an event handler handle_double_click for
instance event picture_dblclick of global class cl_gui_picture. The event handler uses two
explicit output parameters of the event, along with the implicit parameter sender as input parameters.

CLASS picture DEFINITION.
PUBLIC SECTION.
METHODS handle_double_click
FOR EVENT picture_dblclick OF cl_gui_picture
IMPORTING mouse_pos_x mouse_pos_y sender.
ENDCLASS.

CLASS picture IMPLEMENTATION.
METHOD handle_double_click.
...
ENDMETHOD.
ENDCLASS.




1.4.1.6. Redefining Instance Methods
A method declared in a superclass can e redefined in a subclass, provided it is not flagged as
final in the superclass. Redefinition does not change the method ' s interface.

Syntax

METHODS meth [FINAL] REDEFINITION.

This statement is possible only in subclasses .t redefines an inherited instance method meth t
forces the redefinition of method meth in the implementation section of the subclass. The new
implementation in the current class masks the implementation in the superclass. The redefined method
accesses the private components of the redefined class, not the private components with the same
names in the superclass (if any). Within the redefined method, you can use the pseudo reference super-
> meth to call the implementation of the direct superclass.

With the exception of the instance constructor, meth can be any non- final instance method that
is declared in the public or protected visibility area of a superclass of the current class. n particular, meth
can be an abstract method of an abstract superclass. The redefinition must take place within the same
visibility area as the declaration of the method. The interface and type of the method (general or
functional instance method, event handler)are not changed during a redefinition.





21 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s

The redefinition is valid for the subclasses of the redefined class until it is redefined again. You
can continue to redefine a method along a path in the inheritance tree until it is made final .f you use the
addition FINAL during redefinition, the method is final from the current class and can no longer e redefined in
its subclasses.

Note
Each object reference that points to an object in a subclass addresses the redefined methods
regardless of its static type. This also applies to the self-reference me->.
ExampIe
n the example below, method m1 of superclass c1 is redefined i n subclass c2. The original
implementation is called with super->m1 Both methods use the private attribute a1 of the respective class.
The redefined method is executed with reference variable oref, which has static type c1 and dynamic type
c2.

CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS m1 IMPORTING p1 TYPE string.
PRIVATE SECTION.
DATA a1 TYPE string VALUE `c1: `.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.
METHODS m1 REDEFINITION.
PRIVATE SECTION.
DATA a1 TYPE string VALUE `c2: `.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
METHOD m1.
CONCATENATE a1 p1 INTO a1.
WRITE / a1.
ENDMETHOD.
ENDCLASS.


CLASS c2 IMPLEMENTATION.
METHOD m1.
super->m1( p1 ).
CONCATENATE a1 p1 INTO a1.
WRITE / a1.
ENDMETHOD.
ENDCLASS.



1.4.1.7. Static Methods

CLASS-METHODS

The CLASS-METHODS statement declares static methods .You can use static methods with the
class component selector (=>independently of objects. n static methods, you can access only the static
components of its own class or its superclasses if you do not use a component selector .







22 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s

1.4.1.8. GeneraI Static Methods
The most general form of the CLASS-METHODS statement lets you define static methods with
any number of input and output parameters.

Syntax

CLASS-METHODS meth
[IMPORTING {parameteri} [PREFERRED PARAMETER p]]
[EXPORTING {parameteri}]
[CHANGING {parameteri}]
[{RAISING|EXCEPTIONS} {exci}].

This statement declares a general static method, meth. The additions define the parameter
interface of the method and define which exceptions the method can propagate and/or trigger. The syntax
and meaning of the additions are the same as for the general instance methods.

Note
Static methods cannot be redefined, and therefore cannot be made abstract or final either.



1.4.1.9. FunctionaI Static Methods
Functional methods can have any number of input parameters, and have exactly one return value
as output parameter.

Syntax

CLASS-METHODS meth
[IMPORTING {parameteri}]
RETURNING VALUE(r) {TYPE type_spec}|{LIKE dobj_spec}
[{RAISING|EXCEPTIONS} {exci}].

This statement declares a functional static method,meth The additions have exactly the same
syntax and meaning as in the functional instance methods.

ExampIe
The class described below, circle contains two functional methods, circumference and
area and the constants pi.


CLASS circle DEFINITION.
PUBLIC SECTION.
CONSTANTS pi TYPE f
VALUE '3.14159265358979324'.
CLASS-METHODS: circumference IMPORTING r TYPE f
RETURNING value(c)
TYPE f,
area IMPORTING r TYPE f
RETURNING value(a)
TYPE f.
ENDCLASS.

CLASS circle IMPLEMENTATION.
METHOD circumference.
c = 2 * pi * r.
ENDMETHOD.





23 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
METHOD area.
a = pi * r ** 2.
ENDMETHOD.
ENDCLASS.

...

DATA: circ TYPE f,
area TYPE f,
radius TYPE f.
radius = ...
circ = circle=>circumference( radius ).
area = circle=>area( radius ).





1.4.1.10. Static Constructors

Static constructors are methods with the predefined name class_con-structor which are
called automatically before the first use of their class. Static constructors do not have a parameter
interface.

Syntax

CLASS-METHODS cIass_constructor.

This statement is possible only in the public visibility area of the declaration section of a class. t
declares the static constructor of the class, class_constructor.

Each class has a predefined method called class_constructor in its public visibility area.
The explicit declaration enables you to implement its functionality class-specifically. f you do not declare
it explicitly, the static constructor is blank.

The static constructor is executed once for each class and internal session . This execution takes
place automatically before the first access to the class. You access a class whenever you generate an
instance of that class or address a static component with the class component selector.

During the first access of a subclass, the system searches for the next- highest superclass in the
inheritance tree whose static constructor has not been executed yet. The static constructor of the
superclass is then executed, followed successively by each subsequent subclass down to the addressed
subclass.

Like all static methods, the static constructor can access only the static components of its class.
Furthermore, the static constructor cannot address its own class explicitly.



Notes
v Like instance constructors, static constructors represent another exception to the rule that
all public components along a path in the inheritance tree have to line in the same
namespace.
v The execution time for the static constructor is indeterminate. The system merely
guarantees its execution prior to the first access of the class. The static constructor is
usually executed immediately prior to the access of the class. The static constructor is
called at the start of the respective processing block only if a static attribute of the class is





24 / 24 04-09-2003 4:46 PM
)iin +. )vi )iin +. )vi )iin +. )vi )iin +. )vi
A Ab ba ap p O Ob bj je ec ct ts s
accessed. We do not recommend writing programs that require the static constructor to
be executed at a specific time.

ExampIe
The static constructor of the class below sets static attribute access_program to the name of
the program of the first internal session that uses a class during the first access to that class.

CLASS some_class DEFINITION.
PUBLIC SECTION.
CLASS-METHODS class_constructor.
PRIVATE SECTION.
CLASS-DATA access_program TYPE sy-repid.
ENDCLASS.

CLASS some_class IMPLEMENTATION.
METHOD class_constructor.
access_program = sy-repid.
ENDMETHOD.
ENDCLASS.



1.4.1.11. Static Event HandIers

Static event handlers are static methods that are called y an event of a class or an interface. The only
possible formal parameters of an event handler are input parameters that are defined as output parameters of
the event.

CLASS-METHODS meth
FOR EVENT evt OF {class|ifac}
[IMPORTING {pi} [sender]].

You might also like