SAP ABAP Classes - SAP ABAP

What are classes in sap abap?What are its functions?

A class is specifically form of an object which helps to club the data representation and methods that manipulated the data into one set of package. As the number of the students and the teacher who belongs to the same class, likewise the data and functions within a class are called members of the class.

Class Definition and Implementation

In SAPABAP a class is defined with a blueprint for a data type. This blueprint actually means to hold the details of the class name, object of the class and the operations to be performed on such an object. On the whole it gives abstract characteristics of an object such as attributes, fields and properties.

The following syntax shows how to define a class −

If you want to define a class then you have to use the keyword CLASS followed by the class name, DEFINITION and the class body. The class is defined with various components which consist of attributes, methods and events. Whenever the class is declared in a method, the implementation is being done in the class implementation only. Here is the syntax based on how to implement a class −

Note − Implementation of a class contains the implementation of all its methods. In ABAP Objects, the structure of a class contains components such as attributes, methods, events, types, and constants.

Attributes

Attributes are defined as data fields of a class that can include data type such as C, I, F, and N which are declared in the class declaration. These attributes can be categorized into 2: instance and static attributes. An instance attribute include particular state of an object, i.e. holding different states for different objects. An instance attribute is used with the help of DATA statement.

Static attributes define a common state of a class. That is it is not pertaining to particular state but to all the instances of the class. means, if you change a static attribute in one object of a class, that change is applicable to all other objects of the class as well. A static attribute is declared by using the CLASS-DATA statement.

Methods

A method is defined as the behaviour of an object in the class. The methods have the capacity to access any component of the class. As the methods contain parameters, the values are supplied to these parameters when methods are called. The definition of a method is declared in the class declaration and implemented in their declaration and implementation part of a class. The METHOD and ENDMETHOD are the two statements which are used to define the implementation part of a method. The following syntax shows how to implement a method –

In this syntax, <m_name> represents the name of a method. Note − You can call a method by using the CALL METHOD statement.

Accessing Attributes and Methods

Class components contain public, private, or protected visibility sections which controls the components that are to be accessed. The private visibility section of the class restricts access to components from outside the class. Those components are accessed from the methods which are placed inside.

Components that are derived in public visibility section have the capacity to access from any context. At times, all the members of a class may be private. At that time data is defined in private section and the 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.
  • 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

The above code displays the following output −

Static Attributes

To declare a static attribute we use the CLASS-DATA option. 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

This is the program pertaining to the text printed with line number 4 to 8 times. Next, defining a class with class1 and in the public section CLASS-DATA (static attribute) is declared and also a method. After certain changes has made in the class and method, then the user directly access the static attribute in Start-Of-Selection event. After that instance of the class is to be created and call the method.

The above code produces the following output −

static_attribute

Constructors

Constructors are automatically called when an object has been created or accessing the components of a class. There is a special character for the constructors; i.e whenever an object is created it gets triggered. Constructor gets triggered whenever an object is created, but for the triggering of general method the user need to call a method. Here you can see in the following example, two public methods method1 and constructor have declared by the user. Both of these methods perform different operations. So, when the user declares creating an object of the class, the constructor method triggers its operation.

Example

The above code produces the following output −

ME Operator in Methods

Whenever you define a variable of any type in public section of a class, you can use that data type in any other implementation. Firstly, a variable is declared with an initial value in public section. Again you can declare the variable inside the method with another value, but not the same. So, when the user declares the variable inside the method, the system will print the changed value. In order to initiate the first value or previous value, we have to use ‘ME’ operator.

Below you can see the program, that the user declared a public variable text1 and has given some value. In the next case, same variable is declared but with different value (though the variable is same, it has substituted with a different value). In order to call out the previously initiated value inside the method, ME operator is used to write the variable. Thus, previous value will be displayed.

Example

The above code displays the following output −

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAP ABAP Topics