How to use parameters in Methods Object-Oriented ABAP
In Object-Oriented ABAP (OO ABAP), methods play a crucial
role as they define the behavior of objects. Methods can be broadly categorized
into normal methods and special methods, each with its unique characteristics
and usage. Let’s explore these in detail.
1. Normal Methods
Normal methods are the most commonly used methods in OO
ABAP. These methods can either be instance methods or static methods.
a. Instance Method
- Keyword: METHODS
- Instance methods are tied to the specific object
instance of a class. This means that they can only be accessed through an
object of the class.
- Access: They can access all types of components in the
class, including instance attributes, static attributes, constants, and type
definitions.
Key Points:
- Can only be called using an object.
- Can interact with both instance and static components.
b. Static Method
- Keyword: CLASS-METHODS
- Static methods are associated with the class itself,
not with any particular object. They can be accessed either via an object of
the class or directly by using the class name.
- Access: Static methods can only access static
attributes, constants, and type attributes. They cannot interact with instance
attributes of the class.
Key Points:
- Can be accessed using either the class name or an
object.
- Can only work with static components and constants.
2. Special Methods
Special methods in OO ABAP are unique because they are
automatically triggered by certain actions, and they do not need to be
explicitly called by the developer. These methods are the constructor methods.
a. Instance
Constructor
- Keyword: METHODS: constructor
- The instance constructor is automatically called every
time a new object is created. It is object-specific, meaning that each time an
object is instantiated, the instance constructor is executed once.
- Execution: The instance constructor is executed only
once for each object during its lifetime.
Key Points:
- Automatically triggered when an object is created.
- Executes only once per object.
- Declared in the public section of the class.
- It never returns any value.
b. Static
Constructor
- Keyword: CLASS-METHODS: class_constructor
- Unlike the instance constructor, the static constructor
is not tied to any particular object. It is executed once per class, regardless
of how many objects are created.
- Execution: The static constructor is executed either:
- When a static
component of the class is accessed before any object is created.
- Or when the
first object of the class is created, before any static component is accessed.
Key Points:
- Automatically triggered, specific to the class.
- Executed only once in the class’s lifetime.
- Cannot have parameters or exceptions.
- Runs when static components are accessed or when the
first object is created.
3. Method
Parameters
Methods can have parameters that allow data to be passed
in or out. There are four main types of parameters:
a. Importing
Parameters
- Used to pass data into the method.
- By default, importing parameters are obligatory unless
explicitly marked as optional.
- In local classes, you can use the keyword optional to
make the parameter optional. In global classes, you can select the optional
checkbox in the method's parameter settings.
b. Exporting
Parameters
- Used to pass data out of the method.
- Exporting parameters are always optional, meaning they
don’t need to be explicitly provided by the calling program.
c. Changing
Parameters
- These parameters are used when data needs to be passed
into the method, modified within the method, and then passed back out.
- It is a combination of importing and exporting
functionality.
d. Returning
Parameters
- The return parameter is used to pass a single value
back to the calling program.
- Methods with returning parameters are usually simpler
to use when you need to retrieve just one value from the method.
Understanding the different types of methods in OO ABAP
is key to writing efficient and modular code. Instance methods allow
interaction with specific objects, while static methods offer class-level
functionality. Constructor methods, both instance and static, automate the
initialization process, making object creation smoother. With the right use of
parameters, methods can be designed to handle data input and output
efficiently. Mastering these concepts will help you excel in Object-Oriented
ABAP programming and prepare you for common interview questions.