Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

1

Object Oriented Programming - ABAP

OOPs-ABAP - 1

For any programming language to be considered as an "Object Oriented Programming (OOP) language",
it must support the below concepts:
1. Polymorphism
2. Inheritance
3. Encapsulation
4. Abstraction

Like all OOPs languages, ABAP too supports the above 4 basic concepts of OOPs as well as other
related concepts like Object (Instance), Class and methods.

Object:
An object may contain data and/or some parts of processing logic.

State: The data contained by an object is known as the object's state.

Behaviour: The different parts of processing logic of an object is known as the object's behaviour.

Class:
A class is a template/blueprint that describes the state and/or behaviour that the object of its type
support.

A class can consist of attributes and methods:


 Attributes are fields declared inside a class that can hold data and therefore determine the state
of an object that has been created (instantiated) from the class.
 Methods are those parts of a class where code can be provided to execute some processing
logic, which usually act on the attributes. Methods determine the behaviour of an object that has
been created (instantiated) from the class.

When we define a class, we are essentially defining a new data type. A data type defined as a class is
known as ‘Reference Type’.

Two types of classes in ABAP:


Global Class: These types of classes are created using class builder tool and will be available globally
for all ABAP programs in an SAP instance. (Defined using T.Code SE24)

Local Class: These types of classes are created in an ABAP program (SE38) and will be available only
to that program where it is defined.

A class in OOP ABAP is coded in two parts:


1. Class Definition. 2. Class Implementation.

A ‘Class Definition’ contains declarations of fields and methods but not the processing logic. The
processing logic is provided in ‘Class Implementation’. A ‘Class Definition’ must precede ‘Class
Implementation’, i.e., the definition has to be coded first and only then the implementation can be coded.

Every class can have 3 sections (Visibility Sections / Accessibility Sections):


1. Public
2. Protected
3. Private
These sections determine the visibility (access) levels of the ‘components of a class’.
2

Members / Components of a Class: The contents of a class are referred to as ‘members of a class’ or
‘components of a class’, which include:
1. Attributes (also called as ‘Data Components’)
2. Methods
3. Interfaces and
4. Events.

All the above mentioned components of a class (except for Interfaces) can be declared in any of the three
sections (public, protected, & private sections) of a class. Interfaces can be placed only in public section.

Attributes / Data Components: Attributes of a class are the data fields (variables & constants) declared
in a class. Attributes of a class can be declared using the keywords DATA or CLASS-DATA.

Methods: Methods are similar to subroutines or function modules in the sense that they can accept
parameters, contain some code (processing logic) to act on the parameters, except for that they are
contained in a class. Methods of a class can be declared using the keywords METHODS or CLASS-
METHODS.

Public Section: There are no restrictions on the visibility / access of the components placed in public
section. That is the public components are visible / accessible:
1. Within class in which the components are declared,
2. In the subclasses, and
3. Outside the class’ inheritance hierarchy.

Protected Section: There is one restriction on the components placed in protected section. The
components of this section are visible / accessible:
1. Within class in which the components are declared,
2. In the subclasses, but
3. Not accessible outside the class’ inheritance hierarchy.

Private Section: The visibility of the components placed in private section is highly restricted as they are
visible / accessible only within the class in which they are declared and nowhere else.

When we define a structure, then too we are defining a new data type. A structure is a collection of
related fields, but a structure has no processing logic of its own (processing logic: code to assign, read
and/or manipulate data in the fields of a structure).

The below example is based on procedural ABAP code and not on OOPs ABAP
* Defining a Employee as a structure-type in procedural-ABAP using TYPES keyword
TYPES: BEGIN OF TY_EMP,
ENO TYPE I,
ENAME TYPE STRING,
DNO TYPE I,
END OF TY_EMP.

* Declaration of data-objects for Employee using above defined structure-type TY_EMP


DATA: E1 TYPE TY_EMP.

* In procedural ABAP hyphen '-' is used as access operator to access the components of a structure.
E1-ENO = 101.
E1-ENAME = 'AJAY KUMAR'.
E1-DNO = 10.

WRITE: /3 E1-ENO, E1-ENAME, E1-DNO.


3

A class is similar to a structure in a way that it can hold a set of related fields, but in addition to this it can
have its own processing logic in the form of methods.

Note: In OOPs ABAP a combination of two characters hyphen & greater-than '->' has to be used as
access operator to access the components of an object outside its Class definition and implementation.

Syntax for creating objects:


CREATE OBJECT <ref_var> [TYPE <class name>].

 The sample code below explains how to define an Employee as a class to store some data in its
attributes, and thereafter assign and display the data. (This example doesn't cover methods).

REPORT ZOOP_EMP1.

* Definition of a class named EMPLOYEE


CLASS EMPLOYEE DEFINITION. Data Objects (variables) declared inside a
PUBLIC SECTION. class are referred to as "Attributes" or
DATA: ENO TYPE I, "Data Members" of the class.
ENAME TYPE STRING,
DNO TYPE I.
ENDCLASS.

* Declaration of a reference-variable for the above defined class EMPLOYEE


DATA: E1 TYPE REF TO EMPLOYEE.

* Before using the reference-variable E1 to process the employee data you will have to create an object
* for that reference-variable
CREATE OBJECT E1.

* Accessing the attributes (components) of the object referenced by E1 variable to assign data
E1->ENO = 101.
E1->ENAME = 'AJAY KUMAR'.
E1->DNO = 10.

* Accessing the attributes (components) of the object referenced by E1 variable to display data
WRITE: /3 E1->ENO, E1->ENAME, E1->DNO.

NOTE: If you try to access any component of a class through its reference-variable without creating an
object for that reference-variable, such code will result in runtime error; unless the component is a class-
component. [The concept of class-components will be discussed later.]

In the next example, after defining the EMPLOYEE class the attribute components are accessed without
creating an object. Such code doesn't result in any syntax error, but there will be a runtime error when
the program is executed.

A reference variable for which an object is not yet created will hold a NULL value. Unlike procedural
variables which hold some default value (zero or space) depending on its datatype, reference variables
do not hold any default value; not even zero or space. Reference variables by default contain "Nothing",
which is also known as NULL.
4

REPORT ZOOP_EMP2.

CLASS EMPLOYEE DEFINITION.


PUBLIC SECTION.
DATA: ENO TYPE I,
ENAME TYPE STRING,
DNO TYPE I.
ENDCLASS.

DATA: E1 TYPE REF TO EMPLOYEE.


CREATE OBJECT E1.
is not being used here
* Accessing ENO attribute and other component of EMPLOYEE class without creating an object
E1->ENO = 101.
E1->ENAME = 'AJAY KUMAR'.
E1->DNO = 10.

WRITE: /3 E1->ENO, E1->ENAME, E1->DNO.

Save, perform syntax check and activate the above program. After activation when you execute the
program, you will notice that this results in a runtime error with error dump as shown below:

In this line of code you have


attempted to access a component of
the class EMPLOYEE using
reference variable E1, but without
creating an object out of it. Hence,
this line of code has resulted in a
runtime error.
5

An example of class with only methods:

REPORT ZOOP_METHODS.

* Definition of a Class named DATE_AND_TIME with two methods


CLASS DATE_AND_TIME DEFINITION.
PUBLIC SECTION.
METHODS: SHOW_CURRENT_DATE, "Declaration of first method
SHOW_CURRENT_TIME. "Declaration of second method
ENDCLASS.

* Whenever a class is defined with one or more methods, you also need
* to provide an Implementation to that class. In the implementation part
* you have to provide the code that is to be executed whenever the
* method gets called
CLASS DATE_AND_TIME IMPLEMENTATION.

* Implementation for the first method SHOW_CURRENT_DATE


METHOD SHOW_CURRENT_DATE.
WRITE: /3 'Date:', SY-DATUM.
ENDMETHOD.

* Implementation for the second method SHOW_CURRENT_TIME


METHOD SHOW_CURRENT_TIME.
WRITE: /3 'Time:', SY-UZEIT.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
* Declaration of Object reference (variable) for the above defined
* class DATE_AND_TIME
DATA OBJ1 TYPE REF TO DATE_AND_TIME.

** NOTE: If you access the method (Component) without creating an


** object, it will result in runtime error.
* OBJ1->SHOW_CURRENT_DATE( ). "if uncommented will result in runtime error

CREATE OBJECT OBJ1.


Provide at least a single character space between the
OBJ1->SHOW_CURRENT_DATE( ). open and close parenthesis ( and ).
OBJ1->SHOW_CURRENT_TIME( ).

SKIP 2.

* There is an alternate syntax to call methods using CALL METHOD keywords


* When using CALL METHOD command it is not necessary to use parenthesis ( )
* at the end of the call.
CALL METHOD OBJ1->SHOW_CURRENT_DATE.
CALL METHOD OBJ1->SHOW_CURRENT_TIME.

Class Methods:
Class Methods, and other class components, are declared in the DEFINITION part of a class with a prefix
of CLASS-xxx; like CLASS-METHODS, CLASS-DATA.

Class Methods can be accessed like normal methods with -> as access operator on an object-reference
variable. In addition to this, class methods can also be accessed directly using the class name with =>
6

as access operator. Accessing class components with => directly on the class name can be done even
without having created an object of that class; doing so doesn't result in a runtime error.

REPORT ZOOP_CLASS_METHODS.

* Definition of a Class named DATE_AND_TIME with two class-methods


CLASS DATE_AND_TIME DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: SHOW_CURRENT_DATE,
SHOW_CURRENT_TIME.
ENDCLASS.

CLASS DATE_AND_TIME IMPLEMENTATION.


METHOD SHOW_CURRENT_DATE.
WRITE: /3 'Date:', SY-DATUM.
ENDMETHOD.

METHOD SHOW_CURRENT_TIME.
WRITE: /3 'Time:', SY-UZEIT.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.

* Class methods can also be accessed directly using the class name, even
* without creating an object by using => as access operator
DATE_AND_TIME=>SHOW_CURRENT_DATE( ).
DATE_AND_TIME=>SHOW_CURRENT_TIME( ).

Alternatively the method call can done with the


below syntax:
SKIP 2.
CALL METHOD DATE_AND_TIME=>SHOW_CURRENT_DATE.

DATA OBJ1 TYPE REF TO DATE_AND_TIME.

CREATE OBJECT OBJ1.

* Class methods can also be accessed on an object reference after creating


* an object by using -> as access operator
OBJ1->SHOW_CURRENT_DATE( ).
OBJ1->SHOW_CURRENT_TIME( ).
7

NOTE: It is a good coding practice to name local classes with a prefix of LCL_.

Next example explains how to code a class with methods having IMPORTING and EXPORTING
parameters. A method of class is much similar to Function Modules in syntax.

The below class defines and implements two methods, GET_SUM to compute sum of two integer values
and GET_DIFF to compute difference between two integer values.

CLASS LCL_ARITHMETIC DEFINITION.


PUBLIC SECTION.4
METHODS: GET_SUM IMPORTING I_ARG1 TYPE I "declaration of 1st method
I_ARG2 TYPE I
EXPORTING E_SUM TYPE I,
GET_DIFF IMPORTING I_ARG1 TYPE I "declaration of 2nd method
I_ARG2 TYPE I
EXPORTING E_DIFF TYPE I.
ENDCLASS.

CLASS LCL_ARITHMETIC IMPLEMENTATION.


METHOD GET_SUM.
E_SUM = I_ARG1 + I_ARG2.
ENDMETHOD.

METHOD GET_DIFF.
E_DIFF = I_ARG1 - I_ARG2.
ENDMETHOD.
ENDCLASS.

PARAMETERS: P1 TYPE I DEFAULT 55,


P2 TYPE I DEFAULT 8.

START-OF-SELECTION.
DATA: R_ARITH TYPE REF TO LCL_ARITHMETIC,
RES TYPE I.

CREATE OBJECT R_ARITH.

R_ARITH->GET_SUM(
EXPORTING I_ARG1 = P1
I_ARG2 = P2
IMPORTING E_SUM = RES ).
WRITE: /3 'Sum:', RES.

R_ARITH->GET_DIFF(
EXPORTING I_ARG1 = P1
I_ARG2 = P2
IMPORTING E_DIFF = RES ).
WRITE: /3 'Difference:', RES.

Functional & Non-Functional Methods:

Like function-modules, the methods of a class too can have IMPORTING, EXPORTING and CHANGING
parameters. In addition to these type of parameters, the methods of class can also have another type of
parameter; which is RETURNING.
8

Methods with a RETURNING parameter are called Functional methods, and methods without
RETURNING parameters are called Non-functional methods.

If a RETURNING parameter is specified for a method, there can be one-and-only-one parameter of this
type. It is not possible to specify multiple parameters of RETURNING type in a single method. A
parameter specified as RETURNING can only be passed-by-value. In other words, a RETURNING
parameter doesn't support pass-by-reference.

A method can have multiple EXPORTING and/or CHANGING parameters, and each of these parameters
can be handled with either pass-by-reference or pass-by-value.

A method can be either a Functional method or a Non-Functional method, but it cannot be both. If a
RETURNING parameter is specified for a method, then for that method EXPORTING and/or CHANGING
parameters cannot be used.

CLASS LCL_PRODUCT DEFINITION.


PUBLIC SECTION.
METHODS: GET_PRODUCT IMPORTING I_ARG1 TYPE I
I_ARG2 TYPE I
RETURNING VALUE(RE_PROD) TYPE I.
ENDCLASS.

CLASS LCL_PRODUCT IMPLEMENTATION.


METHOD GET_PRODUCT.
RE_PROD = I_ARG1 * I_ARG2.
ENDMETHOD.
ENDCLASS.

PARAMETERS: P1 TYPE I DEFAULT 4,


P2 TYPE I DEFAULT 3.

START-OF-SELECTION.
DATA: R_PROD TYPE REF TO LCL_PRODUCT,
RES TYPE I.

CREATE OBJECT R_PROD.

R_PROD->GET_PRODUCT( EXPORTING I_ARG1 = P1


I_ARG2 = P2
RECEIVING RE_PROD = RES ).

WRITE: /3 'Product:', RES.

Alternate syntax available for calling a functional method:

CALL METHOD R_PROD->GET_PRODUCT EXPORTING I_ARG1 = P1


I_ARG2 = P2
RECEIVING RE_PROD = RES.
(or)

RES = R_PROD->GET_PRODUCT( I_ARG1 = P1 I_ARG2 = P2 ).

You might also like