Summary -

In this topic, we described about the below sections -

What is Class?

A class is a collection of equivalent properties and set of instructions. Class is a template to objects and define objects. In technical, objects are runtime instances of a class.

Any number of objects can be created on a single class. Each instance/object of a class has a unique identity and its own set of values for its attributes.

The data functions within the class are called as class members. The object attributes are defined by the class components that describes the state and behavior of objects.

The below statements define the structure of a class -

  • A class contains components.
  • Each component is assigned to a visibility section.
  • Classes implement methods.

Class Types -

ABAP Classes are two types based on their declaration and visibility. Those are -

Class Description
Global Classes Global classes are accessed by all ABAP programs.
Global classes are defined in the Class Builder (Transaction SE24) in the ABAP Workbench.
Global classes are stored centrally in Repository class library class pools.
Local Classes Local classes are defined within ABAP program.
Local classes can only use in the program where they are defined and not available to other program.
Local classes consist of ABAP source code and should code in between the CLASS and ENDCLASS.

The system first searches for a local class with the specified name when a class in an ABAP program. If it does not find any local classes, then looks for a global class. There is no difference between using a global class or a local class apart from visibility.

Class Definition -

The full class definition consists a declaration part and an implementation part if required. The class declaration syntax is -

CLASS <class-name> DEFINITION. 
	…
	Statements-block
	…
ENDCLASS.

It contains the declaration for all class components (attributes, methods, events). The local class declaration should place at the beginning of the program as it belongs to the global program data.

To declare methods in the class declaration, implementation part should be coded. The syntax for method declaration is -

CLASS <class-name> IMPLEMENTATION.
	…
	Statements-block
	…
ENDCLASS.

The class implementation part contains the all methods implementation of the class.

Class Components -

The class components create its contents. All components are declared in the class declaration part and the components define the object attributes in a class. All the class components are visible within the class.

There are two types of class components – instance components (those exist separately for each object in the class which are instance specific) and static components (those exist only once for the whole class regardless of the number of instances).

Classes can define the below components in ABAP objects -

Class Components Description
Attributes Attributes are internal data fields within a class that are declared with any ABAP data type such as C, I, F and N. The object state is determined by its attributes contents.
Methods Methods are internal procedures in a class and defines the behavior of an object. Methods can access all class attributes that allows them to change the object data content.
Constructors Constructors are special methods that are called automatically while creating an object or instantiated (accessing the class components) first time. Constructor gets triggered when an object is created.
Events Events are functions that are triggered based on the result of a condition. Events are used by Objects or classes to trigger event handler methods in other objects or classes.
Types User-defined ABAP data types defined with a class using the TYPES statement. Types are not instance-specific. Type exist only once for all the class objects.
Constants Constants are special static attributes. Constants can declare by using the CONSTANTS statement. Constants are not instance-specific. Constants exist only once for all the class objects.