Escolar Documentos
Profissional Documentos
Cultura Documentos
The main feature of Object Oriented programming is representing real-time objects in the form
of class objects.
Object Oriented ABAP focus on representing real-time objects of classes.
SAP ABAP Object Oriented programming is available in two flavors.
One is Global Classes and another one is local class.
Global Class
Global Class is an ABAP object which can be accessible via SAP Class Builder, T-code for SAP Class Builder
is SE24.
All of the ABAP programs in an R/3 System can access the global classes.
Local Class
Local classes are classes which are available in ABAP programs, we can access them via ABAP editor
SE38.
Class A class is a user defined data type with attributes, method, events, user defined types, interfaces
etc for particular entity or business application.
Objects Are instances of classes, each object has a unique identity that is memory and its own
attributes.
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 Class:-
3) Events: is a mechanism through which one method of a class can raise method of other class,
without hazard of instantiating the class.
4) Interfaces: are similar to classes which contain methods without any implementation. Mainly used to
extend the scope or functionality of the class.
Static Components These components exist globally for a class and are referred to using component
selector (=>).
In ABAP Objects, the whole class definition is separated into three visibility sections:
PUBLIC.
PROTECTED.
PRIVATE.
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.
Class methods and Creating methods in SAP classes
The importing and exporting parameters of a method will defined under parameters area.
Add a method, click on parameter button and you will go to parameter interface where you can
add parameters to a particular method. (Parameters are method specific).
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.
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.
Select parameters button and declare two parameters as below IM_MATNR-IMPORTING- TYPE-MARA-
MATNR-Material Number EX_MARA-EXPORTING-TYPE-MARA-General Master Data
Double click on CODE icon or Go back to methods and double click on method name and add below code.
*Select material data from mara table into exporting parameter ex_mara (work area) for a material no
im_matnr
INTO EX_MARA
Click on execute.
You will get material details for that material, now class with method is created we have to use it in our
program.
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.
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.
Save, go back to methods, put mouse cursor on GET_MATERIAL_FOR_DATE method, select parameters
button and add below parameters.
Go back to methods and double click on method GET_MATERIAL_FOR_DATE, write the below code.
METHOD GET_MATERIAL_FOR_DATE.
*Get material no, created date, material type, material group, units of measure
*from MARA table
SELECT MATNR ERSDA MTART MATKL MEINS
FROM MARA INTO TABLE ET_MARA.
ENDMETHOD.
Save and activate.
Go to SE38 and create a program and add below code.
REPORT ZSAPN_GET_MATERIALS_FOR_DATE.
START-OF-SELECTION.
Define an event.
Define a method.
Link event and method and convert the method into event-handler method.
Create a triggering method which will raise the event.
Use set handler and register event handler method to a particular instance in the program.
SET HANDLER for. "Here instance is the object created by using create object
o Define an event
Go to Events tab and add an event as below.
NO_MATERIAL-INSTANCE-PUBLIC-No material entered
o Define a method
Go to Methods tab and create a method as below
Go to methods and put cursor on method NO_MATERIAL_HANDLER, click on detail view icon (see below
image).
A pop up will open, provide description, select Event Handler for check box, provide our class name as
ZCL_SAPN_MATERISLS and event name as NO_MATERIAL (Press F4), enter.
You will see an icon (event handler icon), which means the method is event handler method.
Create a triggering method which will raise the event
Event handler method is created, now we have to trigger that event, the event can be triggered by using
below syntax.
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 i
m_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 an event handler method we use
below syntax.
SET HANDLER <INSTANCE>-><EVENT HANDLER METHOD> FOR <INSTANCE>. "Here INSTANCE is object an
d EVENT HANDLER METHOD handler method created in se24
REPORT ZSAPN_CLASS_EVENT.
DATA: LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Class declaration
DATA WA_MARA TYPE MARA. "Work area to store material data
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 without giving any input, the result will be
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, and 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.