Understanding Methods in ABAP Object-Oriented Programming (OOP)
In ABAP Object-Oriented Programming (OOP), methods are
one of the most important components. A method is essentially a collection of
statements designed to perform a specific task. The beauty of methods is that
once they are defined, they can be called and executed multiple times,
enhancing modularity and reusability in code.
In ABAP, traditional modularization techniques include includes,
subroutines, and function modules. Similarly, methods in OOP are a more
structured and reusable way to encapsulate code logic.
Types
of Methods in ABAP OOP
In ABAP, methods are classified into two main categories:
1. Normal Methods
- Instance
Methods: These methods operate on an instance of a class (i.e., they
require object creation).
Keyword: METHODS
<method_name>.
- Static
Methods: These methods belong to the class and can be called without
creating an instance of the class.
Keyword: CLASS-METHODS <method_name>.
2. Special Methods
- Constructor Method: This is a special method used to initialize the object when it is created.
Steps to Create a
Method in ABAP OOP
Let's break down the process of creating and using
methods step by step.
Step
1: Declaration
The first step in defining a method is to declare it in
the class definition. This tells the system what methods the class will have.
- Instance Method:
- These methods
are declared with the keyword METHODS and are executed on a particular instance
of the class.
- Static Method:
- These methods are declared with the keyword CLASS-METHODS and are executed without needing an instance of the class.
Step
2: Implementation
After declaring the method, we move on to the implementation phase. This is where the actual logic of the method is written. Every method has its own implementation block.
Step
3: Calling the Method
To use a method, we call it from either within the class or outside of the class. If the method is instance-based, you need to create an object of the class first. For static methods, you can directly call the method using the class name.
When to Create an
Object?
- Inside the Class: If you are using components (like
data or methods) inside the same class, there is no need to create an object.
- Outside the Class: If you are using components from outside the class, you need to instantiate the class. This is done by creating an object of the class.
Step-by-Step Guide to Creating a Class and Methods in ABAP
Here’s a practical guide to defining and using methods in
a class:
Step
1: Define the Class
First, we define the class and declare its methods and
data components. The methods are specified in the PUBLIC SECTION of the class,
which means they are accessible to external programs or objects.
abap
CLASS lcl_abc
DEFINITION.
PUBLIC SECTION.
DATA : userno TYPE i,
username(50) TYPE c.
METHODS: m1, m2.
ENDCLASS.
- DATA: userno and username are defined to hold the
values we want to work with inside the class.
- METHODS: Two methods, m1 and m2, are declared.
Step
2: Implement the Methods
Now, we write the implementation for each method. In the IMPLEMENTATION
section, the logic of each method is defined.
abap
CLASS lcl_abc
IMPLEMENTATION.
METHOD m1.
userno = 10.
username = 'Arun'.
ENDMETHOD.
METHOD m2.
WRITE: / userno,
/ username.
ENDMETHOD.
ENDCLASS.
- Method m1: Sets the values for userno and username.
- Method m2: Outputs the values stored in userno and username.
Step
3: Call the Methods
To execute the methods, we create an object of the class
and call the methods using that object. This is done in the START-OF-SELECTION
event.
abap
START-OF-SELECTION.
DATA ob TYPE REF TO
lcl_abc.
CREATE OBJECT ob.
CALL METHOD
ob->m1.
CALL METHOD
ob->m2.
- DATA ob: This defines a reference variable for the
class lcl_abc.
- CREATE OBJECT: This instantiates the class, i.e.,
creates an object ob.
- CALL METHOD: This invokes the methods m1 and m2 on the object ob.
Code Example: A
Simple Class with Methods
Here’s the complete code for creating a class with two
methods, implementing them, and then calling them in a program:
abap
REPORT
z_sampleprogram2.
CLASS lcl_abc
DEFINITION.
PUBLIC SECTION.
DATA : userno TYPE i,
username(50) TYPE c.
METHODS: m1, m2.
ENDCLASS.
CLASS lcl_abc
IMPLEMENTATION.
METHOD m1.
userno = 10.
username = 'Arun'.
ENDMETHOD.
METHOD m2.
WRITE: / userno,
/ username.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA ob TYPE REF TO
lcl_abc.
CREATE OBJECT ob.
CALL METHOD
ob->m1.
CALL METHOD ob->m2.
Constructor Method
In many cases, we might want to initialize some data when
an object is created. This is where the constructor method comes into play.
What is a
Constructor Method?
A constructor method is a special method that is
automatically called when an object of the class is created. It is useful for
initializing values for attributes when the object is instantiated.
You define a constructor method like this:
abap
METHODS:
constructor.
You can then implement the logic to initialize data when
the object is created:
abap
METHOD constructor.
userno = 100.
username = 'Default User'.
ENDMETHOD.
Key Points on Methods in ABAP OOP
- Instance
Methods:
- Can
access both instance attributes and static attributes.
- To
call an instance method, you must create an object of the class.
- Static
Methods:
- Can
only access static attributes.
- Can
be called in two ways:
- Via
object: Using the created object of the
class.
- Directly:
Using the class name without creating an object.
Methods in ABAP OOP allow developers to create reusable and modular code. By organizing your logic into methods, you can improve the structure and maintainability of your programs. This blog walked you through the basic concepts of instance and static methods, showed how to define and implement them, and provided a complete code example of using methods in ABAP OOP.
By following these steps, you can create efficient,
maintainable programs with proper encapsulation and modularization.