Table of Contents
In the last article, we have learnt about the concepts and advantages of OOABAP through which we came to know that it mainly revolves around the classes & its object. The last article gives us the overview only, now it’s time to take a deep dive into the major concepts of OOABAP. Let’s start with the SAP ABAP Classes.
Classes in OOABAP defines the object it is having. A class can have infinite number of objects, with a unique combination of state and behavior. Classes contain properties, methods and other things that allow developers to define what the class represents. But a class alone will be of no use, unless it is having some objects, that is why an object is known as the instance of a class. We instantiate an object that points to classes.
The above figure depicts Car as a Class and car models as the Objects of a class ‘CAR’.
Flavors of SAP ABAP Classes:
ABAP Classes can be declared as either globally or locally. Following are the flavors in which ABAP classes are available:
Local class can only be defined within a program, and accessibility of this class is restricted to that program (within which it is defined) only. Defining a local class consist of two things:
a. Class Declaration: This is being done inside a coding block of CLASS DEFINITION. It contains the declaration of all the components (attributes, methods, events) of the class.
Syntax: CLASS ABC DEFINITION.
1. ABC is a name of class.
2. This Class definition should always be at the beginning of the program because the declaration inside this block belongs to global program data.
b. Class Implementation: This part contains the implementation of all the methods declared in CLASS DEFINITION. The CLASS IMPLEMENTATION block of local class is a processing block (meaning consist code of functionality).
Syntax: CLASS ABC IMPLEMENTATION.
Note: If you are declaring methods in CLASS DEFINITION, then the CLASS IMPLEMENTATION of that method is must.
To create a global class, T-code is SE24 (Class Builder) in ABAP workbench. The name itself is suggesting the accessibility of global class, all ABAP programs can use these global classes by instantiating the object of that class.
Structure of Class:
A class structure comprises of 3 things:
- Class Components.
- Visibility section of Components.
- Implemented methods of class.
Components of Class:
It defines the attribute of the objects in a class. While defining a class, components are declared in the declaration part of the class with one of the three visibility section (Public, Private, Protected). The components of classes are available in two types:
- Instance Component – These components exist separately for each object in a class.
- Static Component – Exist only once for whole class.
Following are the components of the class (with each component type):
a. Attribute – variables or constant declared within the class and can be access by all the methods of that class.
- Instance Attribute – Attributes that would be instance specific for an object. It can be declared in local class by using DATA
- Static Attribute – Defines the state of class. It can be declared in local class by using CLASS-DATA
b. Method – Determines the behavior of an object, it provide some functionality.
- Instance Method – It can be declared in local class by using METHODS Accessible by all the attributes of a class.
- Static Attribute – It can be declared in local class by using CLASS-METHODS They can access static attributes only.
c. Events – A mechanism through which one method can raise method of another class.
- Instance Event – It can be declared in local class by using EVENTS This can only be triggered in an instance method.
- Static Event – It can be declared in local class by using CLASS- EVENTS All methods (static and instance) can trigger static events.
Note: Each component name should be unique within a class.
Components of a class have their own visibility section which defines where they can be accessible. There are total three types of visibility:
a. Private – Components will be visible to that class only, where they are defined.
b. Public – Components with this visibility will be available to all.
c. Protected – Components will be visible to that class, where they are defined and to the inherited class.
Types of ABAP Class:
When you create a class through T-Code SE-24(Class Builder), it will ask you which type of class, you want to create. So, there are total five types of classes:
Usual ABAP Class:
As the name itself suggests, usual or a normal class which we create through SE24.
A class to handle the exceptions occurred during runtime or program execution.
Types of Exception classes: Total 3 exceptions classes exist, that are inherited from one super class ‘CX_ROOT’.
While creating an exception class, a check box ‘WITH MESSAGE CLASS’ is there. If u select this, a message class then will be generated. This message class, we can separately generate from t-code SE91.
Suppose, one message is being used frequently in one program. One thing we can do is hardcoding, but that would be against coding standards.
Instead of doing that, we can collect it somewhere (say in a container), and from there we can use it anywhere we want, even in different programs as well. So, a message class is like container only, which holds a bunch of different messages.
Before going for persistent class, let’s first discuss the meaning of ‘persistent’. A data is said to be persistent if it can be preserved beyond the runtime of program.
For a session, ABAP program stays in local ABAP memory till the runtime of that program. To store it permanently, we use persistence service (implemented by persistence class).
Test Class (ABAP Unit):
This class is mainly created for unit testing. Now unit testing of what? When we create any class, we usually check if the required functionality is achieved or not. Before going for any testing, we usually test our code by creating a unit test class.
This unit test class consists some test methods corresponding to the methods of original class (for which testing is being done), and this executes the comparison between the expected and actual values.