Object Oriented ABAP Class Components

ABAP Class components could be Attributes, Methods, Events, Constants, Types, and Implemented interfaces. The components are declared in the definition of the class which makes up its content.

The above-mentioned class components can be further classified as :

  • Instance components
  • Static components

When you create an object of the class, the Instance components exists separately for each object of the class. That is every object will have its own instance components.

Static components are not instance specific and exist only once for whole class, regardless of the number of instances. That is the same static component is available for every instance of the class.

All class components will belong to one of the three visibility sections (PUBLIC SECTION, PRIVATE SECTION, PROTECTED SECTION).

Class components under PUBLIC SECTION define the external interface of the class. Class components under PROTECTED SECTION are not accessible from outside the class, but only from within the class or a child of the class. Class components under PRIVATE SECTION are only accessible from within the class. We discuss more about ABAP CLASS VISIBILITY SECTIONS in a separate article.


ABAP CLASS COMPONENT OVERVIEW

Class ComponentInstance declarationStatic Declaration
AttributesDATACLASS-DATA
MethodsMETHODSCLASS-METHODS
EventsEVENTSCLASS-EVENTS
InterfacesINTERFACESCLASS-INTERFACES
ConstructorCONSTRUCTORCLASS_CONSTRUCTOR

Attributes – These are class variables to store data in the object of a class. It is recommended to declare Attributes under PRIVATE SECTION to hide it from outside class. We use ABAP Data type to declare attributes of a class. We have two types of attributes in ABAP class, Instance Attribute and Static Attribute.

Instance Attribute – are object specific, declared by using DATA keyword in local class.

Static Attribute– are not instance specific, declared by using keyword  CLASS-DATA in local class. Static attributes can be accessed by class name as we as by instances of the class. It is not mandatory to have an object to access static attributes.

Constants – Constants are similar to attributes and holds a constant value throughout the program runtime. Constants are declared using CONSTANTS statement. The value set to a constant cannot be changed.

Methods – Methods are processing blocks of the class. Methods define the behavior of the object. Define methods under PUBLIC SECTION to define the public interface to the class. Methods under PRIVATE SECTION can only be accessed by methods of the class and is used internal modularization. We can define Instance Methods and Static Methods.

Instance Method – can be called only by object of class, and declared by using METHODS keyword in local class

Static Method – can be called by both object of class as well as class name. You don’t need to have an instance of the class to call a static method. It is declared using CLASS-METHOD keyword. Static methods cannot access Instance attributes but can only access static attributes of class.

Special methods – A CONSTRUCTOR is considered a special method as it cannot be called explicitly by using the CALL METHOD statement. We have instance constructor defined using name CONSTRUCTOR and static constructor defined using name CLASS_CONSTRUCTOR.

Events – Events are mechanism through which one method of a class can raise event handler method residing in another class, without the need of instantiating that class. It is one of the important features of ABAP Object Oriented Programming. In ABAP Classes, we can two types of events, Instance Event and Static Event.

Instance event – only Instance method can raise instance event. Instance event are declared using EVENTS keyword in local class.

Static Event– All methods ( static and instance ) can raise static event. Static event is declared in local class by CLASS-EVENTS keyword.

Interface– Interfaces have structure similar to classes, having methods defined but not implemented. Interfaces are used to extend functionality of the class.

Types – just like ABAP programs, own data types can be defined within classes using TYPES statement. Types are not instance-specific, and exist once only for all of the objects in a class.