You are on page 1of 13

Concept of Object Oriented Programming in ABAP

Author: Lokesh Tripathi


Date Written: 20th May 2009
Declaration:
I/We hereby declare that this document is based on my personal experiences. To the best of
my knowledge, this document does not contain any material that infringes the copyrights of
any other individual or organization including the customers of Infosys.
Purpose:
Prerequisites: Experience in ABAP.
Target Readers: SAP ABAP technical consultant

INDEX

S.NO.

Topic

Page no.

OOPs concept

2.

Classes and its types.

Class structure

Object creation

Creating global class

Creating local class

11

References

16

OOPs concept in ABAP.


The Object-oriented programming mainly focuses on objects that represent abstract or
concrete things of the real world.
Uses of Object orientation
1) Easily approachable.

2) Code is re-usable.
3) Data is more secured.
4) Requires less maintenance and hence saves time.
Objects
An object is the structure having data and methods. The attributes of the object is formed
by the data while the services are known as methods.
E.g. Object Customer has all customer related information like name, age, account no. and
methods like salary etc.
Classes
Group of similar objects having similar characteristics is known as classes. We can create
any number of objects based on a single class but each object has a unique identity.
Class types
Classes in ABAP objects can be declared either globally or locally.
1)

Global classes: Global classes are defined in transaction SE24 in the ABAP
workbench. All ABAP programs can access the global classes.

2)

Local classes: The local classes are defined within ABAP program. They can be only
used in the program in which they are defined.

Defining local classes


A local class definition consists of the following parts, i.e.
1) A declaration part: In the declaration part all the data and methods are defined.
All the declarations are defined under block CLASS.ENDCLASS.
Syntax: CLASS <class>
...
ENDCLASS.

DEFINITION.

2) An implementation part: In the implementation part we implement all the


methods defined in the declaration part.
Syntax:

CLASS <class>
...
ENDCLASS.

IMPLEMENTATION.

Class Structure:
1) Components: They are declared in the declaration part of the class.

There are two kinds of components in a class.


a) Instance components: They are unique for each object and defined the
current state of the object. They are accessed through -> operator.
e.g. DATA a1 type string.
Programs can only access the instance components of an object using
references in reference variables like c11->a1 = INSTANCE.
b) Static components: They are unique for each class and independent on
the number of objects and all of the objects in class can access the static
components .They are accessed through => operator.
For declaring static components CLASS-DATA keyword is used.
Syntax: CLASS-DATA
e.g. CLASS-DATA

a1 TYPE string.

It can be accessed as c11=>a1 = 'STATIC', where c11 is the object of some


class.
2) Attributes: They are internal data fields declared in a class. They are of any data
types.
3) Methods: Methods shows the behavior or characteristics of the object in the class.
They can access all the attributes of the class.
Methods are defined in the definition part of a class and implement it in the
implementation part.
Syntax: METHOD <method>.
ENDMETHOD.
Methods are called using the CALL METHOD statement.
4) Events: Objects or classes can use events to trigger event handler methods in other
objects or classes.
Visibility of data and methods in the class
Components have 3 visibility areas
a) Public section: All the components declared in the public section are accessible to
all the methods of the class and any classes that inherit from it.
b) Protected Section: All of the components declared in this section are accessible to
all methods of the class and of classes that inherit from it.

c) Private section: Data declared in the private section can be accessed by the class
only, but not by its subclasses.
These three visibility areas are the basic for one of the important features of object
orientation i.e. Encapsulation.
Syntax:

CLASS<class>DEFINITION.
PUBLIC
SECTION.
PROTECTED
..
PRIVATE

...

SECTION.
SECTION.

...

ENDCLASS.
Object references.
All the data and methods are accessed through object reference in ABAP program. They are
present in the reference variables.
Class references are defined using
DATA: <OBJ> TYPE REF TO <CLASS>.
After creating reference variable we can create an object by the statement
CREATE OBJECT <obj>.
Declaring Methods
We can declare methods in the declaration part of a class.
To declare instance methods we can use this syntax
METHODS <meth> IMPORTING..
EXPORTING
CHANGING..
RETURNING VALUE..
EXCEPTIONS
While declaring static methods we can use
CLASS-METHODS <meth>
Inheritance: It allows us to derive a new class from an existing class.

Syntax: CLASS <subclass> DEFINITION INHERITING FROM <super class>.


New class is called subclass of the class from which it is derived while original class is called
super class.
All of the components of the existing super class are inherited by the subclass. However
only the public and protected components of the super class are visible.
Now lets understand the concept of OOPs with example.
a) First the Global class.
Step 1. Go to transaction SE24.
Give the class name and click the create button.

Give the description and then save it.

Step 2: Declare methods

Go to methods and declare 2 methods i.e. METHOD_FETCH and METHOD_DISPLAY.

Step 3: Declare data


Go to attributes and declare the data and the internal tables.

Step 4: Now define the methods.


Double click the method and write the logic in the method.
Save and activate it.
E.g. Defining METHOD_FETCH

Defining METHOD_DISPLAY

Check and activate the class.


Step 5: Creating program to call the methods of the class.
Go to transaction SE38 and create the program there.

Object creation includes two steps:


i)

DATA: OBJ TYPE REF TO zglobaltest.


It will create the reference variable with reference to the class.

ii)

CREATE OBJECT OBJ.

It will create the object from the reference variable through which we can access all the
methods.
I.e. CALL METHOD obj->METHOD_FETCH.
Output:

b) Now lets understand the concept in local class.

Following are the steps involved in the creation of local classes.


1) Create an ABAP program and declare the class.
2) Implement the class declared and define its methods.
3) Create object of the class.
4) Call the method through this object.
Now lets move step by step:
Step1: Go to transaction se38 and create the program there.

Declare the classes account, credit and debit.


Note: Credit and debit are the child classes of super class account.

Declaring account class.

Now declare the class credit which is the child class of account and is used to credit the
amount.
Class credit

inheriting
class
account

Similarly declare the child class debit which is used to debit the account.

Step2: Implement the classes defined and define the methods.


First implement the super class account.
CLASS account IMPLEMENTATION.
Inside the implementation define all the methods declared in this class.

Defining method init_bseg.

Similarly define method display.

ENDCLASS. End of implementation of class account.


After then implement sub classes credit and debit.
Implementing class credit.

Calling
method
init_bseg
using ->
operator

Similarly implement the class debit and define method debit_amt.


Step3: After defining and implementing the classes create the reference variable and object
of the class to access its methods is created.
Creating reference variable of class credit and debit.

After that create objects for both classes.

Step 4: Call the methods of the sub classes.

Similarly call the methods of subclass debit.

zlocalclass_dem o.doc

PFA the demo program to understand local class concept

References
1) SDN
2) http://help.sap.com/saphelp_46c/helpdata/en/71/a8a77955bc11d194aa0000e835342
3/frameset.htm
3) SAP R/3 Black book.

You might also like