OOABAP Tutorials-4 Creation of Global Class and Using it in Local Program, welcome to SAP ABAP Interview Questions.
OOABAP Tutorials with Examples
sapabapiq.com presenting fourth post in the series of Object Oriented Programming Tutorials. This series of post very helpful for the novice ABAP programmers to learn OOABAP easily with topic wise examples.
Before going to read this article please read previous posts in this series of OOABAP tutorials.
Creation of Global Class and Using it in Local Program
Creation of Global class and using it in a local program |
|
Theme
|
This example will show you how to
create a class globally and use it in your local program
|
Program Description
|
There is a demand to create a global class which will
furnish a list of materials along with their descriptions based on the
material type.
Global class ZGET_MATERIALS will be created. Method
LIST_MATERIALS will belong to this class which will take material type
as an input and will furnish a list of material codes and their descriptions
belonging to that material type.
|
Steps
|
Follow the steps outlined below
to perform the task
|
Step 1. Create the class
from SE24 TCode
* Go
to transaction SE24.
* Enter
the name of the global class you want to create, with ‘Y’ or ‘Z’ at the
beginning.
* Press
Create pushbutton.
* A
dialog window shown above will appear. Check the radio button : Class.
* Press
Enter.
* Another
dialog window shown above will appear. Enter the description for the class.
* Select
from the Instantiation list box whether you want to create the class as
PUBLIC/PROTECTED/PRIVATE/ABSTRACT.
* Check
the radio button for Usual ABAP Class.
* Check
the check box for Final.
* Press
Save push button.
Enter the package name or save it as Local
object.
|
Step 2 : Create the method : LIST_MATERIALS
*
Go to the tab-page : Methods.
* Enter
the details for the method – mention name, type of method(instance/static),
in which visibility section the method will reside and a short description of
the method.
* Check
–off/un check the check box to ensure that the method will be implemented.
Click the pushbutton for Parameters to navigate to
the screen to enter parameters for the method.
There will be one importing
parameter : L_MTART and one exporting internal table : MATERIAL_LIST.
Create entries for them as shown above.
Click the pushbutton : Exceptions
to make entry for Exceptions to be raised by the method.
Enter the name and description
for the exception .
Then, check and activate
the class.
|
Step 3:
Write code for method implementation
Click on the pushbutton : Code( blue colored button) to
implement the method.
An ABAP Editor will open up. Write the logic for code
implementation.
Then, check and activate the code.
Your job of creating a global
class is complete!!!
|
Step 4 : Use the global class created by you in a
local program
REPORT YSUBDEL.
TYPES
: BEGIN OF typ_mat ,
matnr LIKE mara-matnr ,
maktg LIKE makt-maktg ,
END OF typ_mat .
DATA
: it_mat TYPE STANDARD TABLE OF typ_mat ,
x_mat LIKE LINE OF it_mat.
PARAMETERS
: p_mtart LIKE mara-mtart OBLIGATORY.
START-OF-SELECTION.
*
Create object from the global class
DATA : oref TYPE REF TO zget_materials.
CREATE OBJECT oref.
*
Call the method to get list of material code and name
CALL METHOD oref->list_materials
EXPORTING l_mtart = p_mtart
IMPORTING material_list = it_mat
EXCEPTIONS
material_not_found = 2.
if sy-subrc ne 0.
write:/5 'Material not found'.
else.
*
Display the list
loop at it_mat into x_mat.
write:/5 x_mat-matnr ,
x_mat-maktg.
endloop.
endif.
|
|
Output
|
Compile and run the program.
There will be a parameter for material type in the selection screen. Enter a
valid value and get the list of material codes and descriptions.
|
"You found the information helpful and want to say thanks? Your donation is enough to inspire us to do more. Thanks a bunch!"
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.