Object Oriented ABAP Introduction
Object Oriented ABAP Introduction
ABAP programming
Global Class
Global Class is an ABAP object which can be accessible via SAP Class Builder, T-code for
SAP Class Builder is SE24.
Local Class
Local classes are classes which are available in ABAP programs, we can access them via
ABAP editor SE38.
ABAP Classes and It`s components:-
----------------------------------------------
What is a Class ? A class is a user defined data type with attributes, methods, events, user-
defined types, interfaces etc for a particular entity or business application .
What are Objects ? Objects are nothing but instances of classes, each object has a unique
identity that is memory and it`s own attributes.
Declaring Classes and Objects
Syntax: DATA TYPE REF TO . "Declaring global class in ABAP program using type ref
to(type reference to )
CREATE OBJECT . "Create object for the declared instance
Components of a Class:-
Attributes: Attributes are variables, constants declared within a class .
Methods: Methods are coding blocks which provides some functionality .
These methods are similar to Function Modules in ABAP.
Methods can access all attributes of it`s own class.
Methods are defined in definition part and implemented in implementation part.
We can call methods using CALL METHOD statement.
Events: Event is a mechanism through which one method of a class can raise method of other
class, without hazard of instantiating that class.
Interfaces: Interfaces are similar to classes which contain methods Without any
implementation.
Interfaces are mainly used to extend the scope or functionality of the class.
Public section: Data declared in public section can be accessed by the class itself, by its
subclasses as well as by other users outside the class.
Protected section: Data declared in the protected section can be accessed by the class itself,
and also by its subclasses but not by external users outside the class.
Private Section: Data declared in the private section can be accessed by the class only, but
not by its subclasses and by external users outside the class.
AV_NAME-INSTANCE-PUBLIC-TYPE-CHAR25.
Transaction code for SAP class builder is SE24. SAP class builder(SE24) is used to create,
display and change SAP classes.
Go to SE24 and provide some class example ex: CL_ABAP_CHAR_UTILITIES, to see what
are the components of class.
Properties: The properties tab contains class properties like created user name, last changed
user name, package etc.
Interfaces: Contains the list of the interfaces that are implemented in this class( will discuss in
later lessons).
Friends: contains friend classes ( we will discuss later lessons).
Attributes: The attributes tab contains the list of attributes declared in that class.
Methods : Contains the methods with importing and exporting parameters and with some
business functionality (source code).
Events: Contains events declared and implemented in that class.
Types: The types tab contains user defined type decelerations.
Aliases: Contains alias names for interface methods ( will discuss in later lessons).
To write source code, click on methods tab and double click on method name (METHOD in
the above example), you will go to source code editor where you can add code.
This is how we create methods in SAP classes, we will learn some advanced concepts of
using methods in next lessons.
Requirement: Create a class method to get material details for a material number.
: Requirement analysis For the above requirement we need to create a class method to get
details of a material for a material input, for this method the importing parameter is material
no (to pass material input to method) and exporting parameter is of type MARA(work area
one material no contains only one record in MARA).Go to SE24, provide class name as
ZCL_SAPN_MATERIALS.
You will get material details for that material, now class with method is created we have to
use it in our program.
Using Class method in SE38 Program
Go to SE38 and create a program ZSAPN_GET_MATERIAL_DETAILS and follow
below steps to add code.
Step1: Declare class and create object.
REPORT ZSAPN_GET_MATERIAL_DETAILS.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.
START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.
Requirement: Develop a report to display list of materials for a material type, use Object
Oriented ABAP to get materials.
Requirement analysis: For this requirement we need to get materials from MARA(material
Master) table for the specified material type, a material type may have more than one
materials, so we have to get materials into an internal table.In SAP classes we don`t have
options to directly specify tables, for this one we need to create table type in SE11(Data
Dictionary).
Learners directly coming for this lesson, please refer previous lesson creating class method
to display materials as we are adding one more method for the same class.
Before creating method to get material details for a material type, we need to create a table
type in SE11 .
Go to SE11, select data type radio, provide name as ZSAPN_MARA, create.
Save, go back to methods and double click on GET_MATERIALS_FOR_TYPE and add the
below code.
START-OF-SELECTION.
CALL METHOD LO_MATERIALS->GET_MATERIALS_FOR_TYPE "call method to
get materials
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.
Program input.
Program output.
Requirement: Develop a report to display material number(MATNR), material type(mtart),
material group(MAKTL) and unit of measure(MEINS) for a given date( Created Date).
Requirement analysis: For this requirement we need to get limited fields (MATNR,
MTART, MATKL, MEINS, ERSDA from MARA (material Master) table for the specified
date(ERSDA-Created date), materials for the given date may be more than one so we need to
use tables and we need to get limited fields(columns) only (no need to get all fields data from
MARA), for this one we need to use concept of user defined table in SAP class method.
Learners directly coming for this lesson, please refer previous lesson creating class method
to display materials as we are adding one more method for the same class.
Go to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created
this class see previous lesson) and click change.
METHOD GET_MATERIAL_FOR_DATE.
*Get material no, created date, material type, material group, units of measue
*from MARA table
SELECT MATNR ERSDA MTART MATKL MEINS
FROM MARA INTO TABLE ET_MARA.
ENDMETHOD.
START-OF-SELECTION.
SET HANDLER for . "here instance is the object created by using create object
Using of event handler methods in SAP classes, implementing event handler method in
OOABAP
+-
Define an event
Go to Events tab and add a event as below.
NO_MATERIAL-INSTANCE-PUBLIC-No material entered
Define a method
Go to Methods tab and create a method as below
NO_MATERIAL_HANDLER-INSTANCE-PUBLIC-Event Handler Method.
Save, double click on the method and add some code.
WRITE:/ 'NO material entered'.
We will trigger the event for the method GET_MATERIAL_DTAILS (we previously
created Get material details ), double click on the method GET_MATERIAL_DTAILS and
add the below code.
METHOD GET_MATERIAL_DTAILS.
*Select material data from mara table into exporting parameter ex_mara (work area) for a
material no im_matnr
IF IM_MATNR IS INITIAL .
RAISE EVENT NO_MATERIAL.
ELSE.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
ENDIF.
ENDMETHOD.
Use set handler and register event handler method to a particular instance in the program
Now we have to register this event in our SE38 program, to register a event handler method
we use below syntax.
SET HANDLER <INSTANCE>-><EVENT HANDLER METHOD> FOR <INSTANCE>.
"here INSTANCE is object and EVENT HANDLER METHOD handler method created in
se24
START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
Now execute the program with out giving any input, the result will be
Assignment for you: Create and implement a event and event handler method for material
not found in the above class, if user provided the input but the data is not there in MARA
table, raise an event.
CONSTRUCTOR.
These methods can only have importing parameters, there will not be any exporting
parameters.
The name of the CONSTRUCTOR method must be CONSTRUCTOR only.
CLASS CONSTRUCTOR (Also called as STATIC CONSTRUCTOR).
It is a type of constructor, this method will be executed automatically whenever a first call to
the class is made, the first call may be through instance or through class.
These CLASS CONSTRUCTORS are mainly used to set the default values globally i:e
irrespective of instances, these methods will not have any importing and exporting
parameters.These methods will be executed only once.
Name of the CLASS CONSTRUCTOR must be CLASS_CONSTRUCTOR.
Using CONSTRUCTOR method in SAP Classes to set global default value for the call
+-
This CONSTRUCTOR method is very useful in setting default value in a class, the below is
the example of using CONSTRUCTOR method in a SAP Class.Requirement: Display for
material description for a material, and depends upon language selected.
All material descriptions are stored in MAKT table, for a material there may be different
descriptions example for English one description, for German one description etc, we need to
get description based on the language, for this one we create a class method for re-
usablity(the same method can be used in different programs to get descriptions based on the
selected languages).
Go to SE24,provide class ZCL_SAPN_MATERIAL and click on change.
Once you press enter, you will see a symbol (Constructor symbol), it indicates that the
method is a constructor method.
Go to methods and double click on CONSTRUCTOR method and add below code.
Save and activate, go to methods tab and add one more method
GET_MATERIAL_DESCRIPTIONS to get material descriptions.
METHOD GET_MATERIAL_DESCRIPTIONS.
ENDMETHOD.
Using CONSTRUCTOR method with ABAP program
Go to SE38 and create a program ZSAPN_MATERIAL_DESCRIPTION and follow below
steps.
Step1: Data declarations
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Declare class
DATA : WA_MAKT TYPE MAKT. "declare work area for makt
PARAMETERS P_MATNR TYPE MARA-MATNR. "material input
Step2: Create object for material class, the object can be created using ABAP patterns also.
To create object, click on pattern button.
START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
WRITE : WA_MAKT-MATNR, WA_MAKT-MAKTX.
Execute the program, provide the material no, you will get out put where language = 'E' only
(Constructor method handles this).
This is a type of CONSTRUCTOR, this method is executed whenever a first call to the class
is made, the call may be through instance or through class name.
These are also called as STATIC CONSTRUCTORS, the name must be
CLASS_CONSTRUCTOR.
If you are coming directly for this lesson we highly recommend go through previous
lesson Using CONSTRUCTOR method in SAP classes for better understanding.
Example: We will add a class constructor to make default material type as 'FERT'(Finished
Product)
Follow the below steps to create a class constructor.
Go to SE24, provide the class name ZCL_SAPN_MATERIALS, click on change.
Go to attributes tab and add an attribute to make default material type.
Here we take a small example to test this one.The below on is a simple example and explains
you a lot.
REPORT ZSAPN_CLASS_CONSTRUCTOR.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Declare class
It is a concept by which the same method names will behave differently in different classes i.e
each method will have its own implementation in different different classes but with the same
name.
Interface is one of the concept in Object Oriented ABAP to achieve Polymorphism.
What is an interface in Object Oriented ABAP ?
Interfaces are independent structure which are used in a class to extend the
functionality of a class.
Interfaces contains methods without any implementation. Where as a class contains
methods with implementation.
We need to define an interface with the required method names in SE24 TCODE.
The interfaces can be used by no of classes to extend the functionality of the class.
To implement an interface in a class just give the name of interface under the
'Interfaces' tab.
All the interface methods will be automatically copied to the classes in a particular
class without effecting the other classes.
The main use of interfaces is re-usability and maintain standard project framework.
In the above diagram, application is business application, Class A, Class B, Class C, Class D
and Class E are independent classes, all these classes are using one Interface called
Interface( in diagram).
In all the classes,the methods names are same but the implementation will be different from
one class to another class thereby achieving the concept called POLYMORPHISM.
Polymorphism is implemented in the from of interface.
As per SAP Standard documentation all the standard interfaces in SAP starts with IF_ and all
the custom interfaces starts with ZIF_ , in the below example we will be learning how to
create a use interfaces in SAP.
All the custom classes is SAP starts with ZCL_ and all the custom interfaces will start with
ZIF_, based on the provided name (at the time of creation), SAP will automatically determine
and creates (interface or class).
Go to SE24, provide name as ZIF_SAPN_MATERIAL_EXAMPL, create.
Provide description, save, save it in a local object.
Now go to methods tab, you can see all the methods in the interface are automatically copied.
Save, double click on each method and create your own implementation.
Double click on ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS, and
add below code.
Double click on
ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS, add below
code
SELECT * FROM MAKT INTO EX_MAKT
WHERE MATNR = IM_MATNR
AND SPRAS = 'E'. "English description only
ENDSELECT.
START-OF-SELECTION.
CALL METHOD LO_CLASS-
>ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS-
>ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
WRITE :/ 'Material Details - ' COLOR 5, WA_MARA-MATNR, WA_MARA-MTART,
WA_MARA-MBRSH, WA_MARA-MATKL.
WRITE :/ 'Material Descriptions - 'COLOR 6, WA_MAKT-MATNR, WA_MAKT-
MAKTX, WA_MAKT-SPRAS.
Similarly create another class and repeat the same process, we can add interfaces into multiple
classes.
If the final check box is selected, we can not use this class for inheritance, to use inheritance
remove the final check box.
A pop up will open provide description, un-select final check box, save and save as local
object.
Go to methods tab, provide method name as GET_MATERIAL_DETAILS-INSTANCE-
PUBLIC.
Go back to methods, double click on method name and add below code.
METHOD GET_MATERIAL_DETAILS.
ENDMETHOD.
A pop up will open provide description, click on create inheritance icon(see below image),
provide super class name.
Save, save as a local object, now you can see the inherited method, using inheritance we can
overwrite the method implementation to do this click on redifinition icon (see image below)
We will add additional functionality to get material description details along with material
details, go to attributes tab and add a attribute ls_makt-instance-public-makt.
Go back to methods, doublle click on method, uncomment the inherited code, pass parameters
and add additional code to get material descriptions.
METHOD GET_MATERIAL_DETAILS.
ENDMETHOD.
START-OF-SELECTION.
CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "get material details
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.