Você está na página 1de 15

Object Oriented:

 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:-

1) Attributes: are variables, constants declared within a class.

2) Methods: are coding block which provide some functionality.


i) Similar to function module.
ii) Can access all attributes of its own class.
iii) Defined in definition part and implemented in implementation part.
iv) Called using Call Method statement.

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.

Instance and Static Components


Instance Components These components exist separately in each (Object) of the class and are referred
using instance component selector (->).

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

1. What is a method in a Class?


 Methods are coding blocks of a class, which can provide some business functionality (ex: read
material data etc.), these methods are similar to Function Modules.
 Methods can access all attributes of a class (defined under attributes tab), can access user
defined types (declared under types tab).
 The methods can be called using key word CALL METHOD in SAP ABAP programs.
2. Uses of methods in SAP classes?
 These methods can be reusable in multiple ABAP programs, a class may contain more than one
method.
 We will define methods under methods tab of a class, to define a method provide a method
name, level (instance or static), visibility (Public, Private, Protected) and method description.


 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.

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.

Provide description, save.


Save, save it in a local object, go to methods tab and declare a method. GET_MATERIAL_DETAILS-
INSTANCE-PUBLIC-Get Material details

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

SELECT SINGLE * FROM MARA

INTO EX_MARA

WHERE MATNR = IM_MATNR.


Save, activate and execute the method (press F8).

Click on execute.

Provide a material no and execute.

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.

DATA: LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.


CREATE OBJECT LO_MATERIAL.
Step2: Program declarations.

PARAMETERS: P_MATNR TYPE MARA-MATNR.


DATA: WA_MARA TYPE MARA. "Work area to store material details

Step3: Call class method and print output.

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.

Final code will be

REPORT ZSAPN_GET_MATERIAL_DETAILS.
DATA: LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.

PARAMETERS: P_MATNR TYPE MARA-MATNR.


DATA: WA_MARA TYPE MARA.
CREATE OBJECT LO_MATERIAL.

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.

Go to methods tab and add a method with name GET_MATERIALS_FOR_DATE-INSTANCE- PUBLIC-Get


materials for a date.

We can declare user-defined types under types tab, go to types tab.


Click on Direct Type entry icon, save, it will take you to a editor
Don' change anything, just replace types TY_MARA. With the below code.

TYPES: BEGIN OF TY_MARA,


MATNR TYPE MARA-MATNR,
ERSDA TYPE MARA-ERSDA,
MTART TYPE MARA-MTART,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.

TYPES: TT_MARA TYPE TABLE OF TY_MARA.

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.

TYPES: BEGIN OF TY_MARA,


MATNR TYPE MARA-MATNR,
ERSDA TYPE MARA-ERSDA,
MTART TYPE MARA-MTART,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA: IT_MARA TYPE TABLE OF TY_MARA.
DATA: WA_MARA TYPE TY_MARA.

DATA: LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.


PARAMETERS: P_DATE TYPE MARA-ERSDA.
CREATE OBJECT LO_MATERIAL.

START-OF-SELECTION.

CALL METHOD LO_MATERIAL->GET_MATERIAL_FOR_DATE


EXPORTING
IM_DATE = P_DATE
IMPORTING
ET_MARA = IT_MARA.

LOOP AT IT_MARA INTO WA_MARA.


WRITE: / WA_MARA-MATNR, WA_MARA-ERSDA, WA_MARA-MTART, WA_MARA-MATKL, WA_MARA-
MEINS.
ENDLOOP.
 What are events in SAP Classes?
Event is a mechanism by which method of one class can raise method of another class, without the
hazard of instantiating that class.
Follow the below steps to add an event

 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.

Use below syntax to register an event handler method for a instance

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

NO_MATERIAL_HANDLER-INSTANCE-PUBLIC-Event Handler Method.


Save, double click on the method and add some code.

WRITE: / 'NO material entered'.

Save and activate immediately.


Link event and method and convert the method into event-handler method.
Now we have to link the method to event to make the method as event handler.

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.

RAISE EVENT <EVENT NAME>.

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

Go to SE38, create a program ZSAPN_CLASS_EVENT and write the below code

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

PARAMETERS P_MATNR TYPE MARA-MATNR. "Material input


CREATE OBJECT LO_MATERIAL. "Create object for material class
SET HANDLER LO_MATERIAL->NO_MATERIAL_HANDLER FOR LO_MATERIAL. "Register event handler met
hod

START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

WRITE: WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.

Now execute the program without giving any input, the result will be

What is a constructor in a class?


CONSTRUCTOR is a special type of method, it is executed automatically whenever an object is created or
instantiated.
These methods are mainly used to set default values in classes.
CONSTRUCTORS are two types in SAP classes.

1. CONSTRUCTOR (Instance Constructor).


2. CLASS CONSTRUCTOR (Static Constructor).

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.

Você também pode gostar