The classes are templates for objects. An abstract
description of an object is the class. You could say it is a set of
instructions for building an object. The attributes of objects are defined by
the components of the class, which describe the state and behavior of objects.
Local and Global Classes
In ABAP Objects, classes can be declared either globally or
locally. You define global classes and interfaces in the Class Builder
(Transaction SE24.} in the ABAP Workbench. In the R/3 Repository, they are
stored centrally in class pools in the class library. In an R/3 System, all of
the ABAP programs can access the global classes. The local classes are defined
within an ABAP program. Local classes and interfaces can only be used in the
program in which they are defined. When you use a class in an ABAP program, the
system first searches for a local class with the specified name. If it does not
find one, then it looks for a global class. Apart from the visibility question,
there is no difference between using a global class and using a local class.
However, there is a significant difference in the way that
local and global classes are designed. If you are defining a local class that
is only used in a single program, then to define the outwardly visible
components so that it fits into that program is usually sufficient. On the
other hand, global classes must be able to be used anywhere. Since the system
must be able to guarantee any program using an object of a global class, it can
recognize the data type of each interface parameter and then certain
restrictions are applied at the time of defining the interface of a global
class.
Defining Local Classes
Local classes consist of ABAP source code, where the ABAP
statements CLASS...ENDCLASS are enclosed. A complete class definition consists
of the following parts, a declaration part and, if required, an implementation
part. It is found that the declaration part of a class <class> is a
statement block:
It contains the declaration for all components (attributes,
methods, events) of the class. When you define local classes, the declaration
part belongs to the global program data. Therefore, it should be placed at the
beginning of the program. If you declare methods in the declaration part of a
class, then you must write an implementation part for it. This consists of a
statement block:
CLASS <class> IMPLEMENTATION
¦
ENDCLASS
The implementation part of a class actually contains the
implementation of all methods of the class because it is observed that the
implementation part of a local class is a processing block. Therefore,
subsequent coding itself is not a part of a processing block and is not
accessible.
Class Structure
A class contains components where each component is assigned
to a visibility section. Moreover, the classes implement methods. Let us define
these components one by one.
Components
This is the first component and is declared in the
declaration part of the class. The components define the attributes of the
objects in a class. When you define a class, each component is assigned to one
of the three visibility sections. This defines the external interface of the
class and all of the components of a class are visible within the class. All
components are in the same namespace. This means that all components of the
class must have names that are unique within the class. There are two kinds of
components in a class:
One that exists separately for each object in the class.
The other consists of those that exist only once for the
whole class, regardless of the number of instances.