35 - SAP ABAP - Classes
35 - SAP ABAP - Classes
A class definition starts with the keyword CLASS followed by the class name,
DEFINITION and the class body. The definition of a class can contain various components
of the class such as attributes, methods, and events. When we declare a method in the
class declaration, the method implementation must be included in the class
implementation. The following syntax shows how to implement a class −
Attributes
Attributes are data fields of a class that can have any data type such as C, I, F, and N.
They are declared in the class declaration. These attributes can be divided into 2
categories: instance and static attributes. An instance attribute defines the instance
https://www.tutorialspoint.com/sap_abap/sap_abap_classes.htm 1/7
Page 2 of 7
specific state of an object. The states are different for different objects. An instance
attribute is declared by using the DATA statement.
Static attributes define a common state of a class that is shared by all the instances of
the class. That is, if you change a static attribute in one object of a class, the change is
visible to all other objects of the class as well. A static attribute is declared by using the
CLASS-DATA statement.
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
Methods
A method is a function or procedure that represents the behavior of an object in the
class. The methods of the class can access any attribute of the class. The definition of a
method can also contain parameters, so that you can supply the values to these
parameters when methods are called. The definition of a method is declared in the class
declaration and implemented in the implementation part of a class. The METHOD and
ENDMETHOD statements are used to define the implementation part of a method. The
following syntax shows how to implement a method −
METHOD <m_name>.
..........
..........
ENDMETHOD.
In this syntax, <m_name> represents the name of a method. Note − You can call a
method by using the CALL METHOD statement.
Components defined in the public visibility section can be accessed from any context. By
default all the members of a class would be private. Practically, we define data in private
section and related methods in public section so that they can be called from outside of
the class as shown in the following program.
The attributes and methods declared in Public section in a class can be accessed
by that class and any other class, sub-class of the program.
When the attributes and methods are declared in Protected section in a class,
those can be accessed by that class and sub classes (derived classes) only.
https://www.tutorialspoint.com/sap_abap/sap_abap_classes.htm 2/7
Page 3 of 7
When the attributes and methods are declared in Private section in a class, those
can be accessed by only that class and not by any other class.
Example
Report ZAccess1.
CLASS class1 Definition.
PUBLIC Section.
Data: text1 Type char25 Value 'Public Data'.
Methods meth1.
PROTECTED Section.
Data: text2 Type char25 Value 'Protected Data'.
PRIVATE Section.
Data: text3 Type char25 Value 'Private Data'.
ENDCLASS.
Start-Of-Selection.
Data: Objectx Type Ref To class1.
Create Object: Objectx.
CALL Method: Objectx→meth1.
Write: / Objectx→text1.
Public Method:
Public Data
Protected Data
Private Data
Public Data
https://www.tutorialspoint.com/sap_abap/sap_abap_classes.htm 3/7
Page 4 of 7
Static Attributes
A Static attribute is declared with the statement CLASS-DATA. All the objects or
instances can use the static attribute of the class. Static attributes are accessed directly
with the help of class name like class_name⇒name_1 = 'Some Text'.
Example
Following is a program where we want to print a text with line number 4 to 8 times. We
define a class class1 and in the public section we declare CLASS-DATA (static attribute)
and a method. After implementing the class and method, we directly access the static
attribute in Start-Of-Selection event. Then we just create the instance of the class and
call the method.
Report ZStatic1.
CLASS class1 Definition.
PUBLIC Section.
CLASS-DATA: name1 Type char45,
data1 Type I.
Methods: meth1.
ENDCLASS.
Start-Of-Selection.
class1⇒name1 = 'ABAP Object Oriented Programming'.
class1⇒data1 = 0.
Data: Object1 Type Ref To class1,
Object2 Type Ref To class1.
https://www.tutorialspoint.com/sap_abap/sap_abap_classes.htm 4/7
Page 5 of 7
Constructors
Constructors are special methods that are called automatically, either while creating an
object or accessing the components of a class. Constructor gets triggered whenever an
object is created, but we need to call a method to trigger the general method. In the
following example, we have declared two public methods method1 and constructor. Both
these methods have different operations. While creating an object of the class, the
constructor method triggers its operation.
Example
Report ZConstructor1.
CLASS class1 Definition.
PUBLIC Section.
Methods: method1, constructor.
ENDCLASS.
Method constructor.
Write: / 'Constructor Triggered'.
EndMethod.
ENDCLASS.
Start-Of-Selection.
https://www.tutorialspoint.com/sap_abap/sap_abap_classes.htm 5/7
Page 6 of 7
Constructor Triggered
ME Operator in Methods
When you declare a variable of any type in public section of a class, you can use it in any
other implementation. A variable can be declared with an initial value in public section.
We may declare the variable again inside a method with a different value. When we write
the variable inside the method, the system will print the changed value. To reflect the
previous value of the variable, we have to use ‘ME’ operator.
In this program, we have declared a public variable text1 and initiated with a value. We
have declared the same variable again, but instantiated with different value. Inside the
method, we are writing that variable with ‘ME’ operator to get the previously initiated
value. We get the changed value by declaring directly.
Example
Report ZMEOperator1.
CLASS class1 Definition.
PUBLIC Section.
Start-Of-Selection.
Data objectx Type Ref To class1.
https://www.tutorialspoint.com/sap_abap/sap_abap_classes.htm 6/7
Page 7 of 7
https://www.tutorialspoint.com/sap_abap/sap_abap_classes.htm 7/7