© Rehan Zaidi 2019
R. ZaidiSAP ABAP Objectshttps://doi.org/10.1007/978-1-4842-4964-2_4

4. Class Builder

Rehan Zaidi1 
(1)
Dubai, United Arab Emirates
 

Global classes are defined in the Class Builder, which has the transaction code SE24. Global classes and interfaces can be accessed by all ABAP programs in the R/3 system.

In this chapter, we first see how we can use the Object Navigator as an alternative transaction for creating global classes. We then move on to the Class Builder, where we will spend some time exploring its various tabs, particularly the Methods tab. We take a brief look at the Class Browser, and then return to the Class Builder, where we learn to test instance methods and view some examples.

Finally, we see a number of examples using the transaction SE24 that will be tested using the transaction itself rather than having to create a program for them.

Transaction SE80

One easy way to create a new global class is by using the Object Navigator, which has the transaction code SE80. The navigator is split into two areas: the navigation area and the tool area. A screenshot of the Object Navigator is shown in Figure 4-1.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig1_HTML.jpg
Figure 4-1

The Object Navigator

The left side of the window is the navigation area, and right side is the tool area. It also makes it easy for us to use the navigation area in order to navigate through the various components of the global class.

Follow these steps to create a global class using SE80:
  1. 1.

    To create a new global class, use the context menu in the navigation area.

     
  2. 2.

    First select the package node or select the Class Library node within the package and right-click it. From the context menu that appears, choose the Create option. A dialog box will open.

     
  3. 3.

    Input the class name and description, select the Usual ABAP Class option, and uncheck the Final checkbox, as shown in Figure 4-2. Click the Save button.

     
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig2_HTML.jpg
Figure 4-2

Create Class dialog box

The global class will then appear in the Class Builder (in the right pane), which you can find in the tool area of the Object Navigator. It will display various tabs like Interface, Attributes, Methods, etc., as you can see in Figure 4-3.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig3_HTML.jpg
Figure 4-3

Creating a class

Here, we can define any new method and its parameters, the class attributes, and other components. The steps from this point on are the same as the ones when using the transaction SE24.

The Class Builder and Its Features

As mentioned earlier, transaction SE24 is the main global Class Builder. With this transaction, we can create, display, and modify global classes and interfaces. Together, these form the Central Class Library, which is accessible throughout the SAP system. Class Builder is used to define or create new custom global classes. It may be used to modify the existing custom SAP ABAP classes and interfaces. Using the Class Builder, it is also possible to implement inheritance of classes and the redefining of the inherited methods.

To create a new global class or interface, start the name with Y or Z (the convention followed for all custom developments). This will distinguish it from a standard class or interface.

We may use the transaction SE24 to display the standard SAP classes. Now, let’s look at one of the standard ABAP classes. Let’s take the example of CL_ABAP_REGEX. We start with the initial screen of the Class Builder. The Class Builder (SE24) initial screen is shown in Figure 4-4.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig4_HTML.jpg
Figure 4-4

Class Builder

Enter the name of the CL_ABAP_REGEX class in the Object Type field (see Figure 4-5) and click Display.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig5_HTML.jpg
Figure 4-5

Class Builder with a standard SAP class

The screen—with the Properties, Interfaces, Friends, Attributes, Methods, Events, Types, and Aliases tabs—appears. See Figure 4-6.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig6_HTML.jpg
Figure 4-6

Display mode

These methods can be called from any ABAP program.

Class Builder Tabs

A global class includes several components, all of which have a visibility—public, private or protected. The various tabs on the Class Builder are shown in Figure 4-7.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig7_HTML.jpg
Figure 4-7

Global class methods

The tabs on the Class Builder transaction SE24 are as follows:
  • Properties . This tab includes the properties, such as a short description of the class, the name of the user who created it, the date of the last change by a user, the package name, etc.

  • Interfaces. This shows the list of interfaces that the class implements.

  • Attributes. In this tab, we specify the list of attributes of the class. Attributes include data, constants, and types.

  • Methods. The tab has two subtabs: Parameters and Exceptions. Parameters include importing, exporting, changing, and returning. These may be based on any data types or dictionary types, or may refer to any objects. Exceptions are exceptional situations that do not allow a method to proceed normally during execution. These may be raised within the code and may be used to avoid the method from resulting in a runtime error (and short dump).

  • Events. On this tab, we can define several events for the class. A corresponding method can be triggered when an event occurs.

  • Types. On this tab, we define or declare any user-defined types, which we can then use in method parameters. To declare a user-defined type, go to the Types tab and input the type name, its visibility, an associate type, and a description, then click Save. See Figure 4-8.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig8_HTML.jpg
    Figure 4-8

    Types tab

    When you click on the direct type entry icon (the yellow arrow), it will take you to the ABAP editor where the type is declared. You can now use this user-defined type in the method parameters.
    TYPES lty_num1 TYPE any.
    TYPES lty_num2 TYPE any.
  • Aliases. On this tab, we can declare any aliases. These are only used in conjunction with interfaces and may be used to form short names to components existing in an interface that is implemented by a given class.

Useful Functions of the Class Builder

In this section, we discuss the various functions within the Class Builder.

Where-Used Lists

This is one of the functions used in the Class Builder as well as in the methods of a class. As its name suggests, the where-used list shows every place (in a program, class or enhancement) where a class or method is used.

To create a where-used list for a class (e.g., CL_SALV_TABLE), go to Utilities ➔ Where-Used List (CTRL+Shift+F3), as shown in Figure 4-9.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig9_HTML.jpg
Figure 4-9

Creating a where-used list

A dialog box will appear with the class/interface name CL_SALV_TABLE, as shown in Figure 4-10. Select the top three options to see which program or classes/interfaces use them, then click to continue.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig10_HTML.jpg
Figure 4-10

Where-Used List dialog box

A dialog box appears with a message, as shown in Figure 4-11. Click Yes.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig11_HTML.jpg
Figure 4-11

Confirm your where-used list

The output will display a hit list showing each place the CL_SALV_TABLE class is used or defined in the program. In this case, there are around 149 programs using this class. From this hit list, you can select the location of the object’s exact usage.

In this way, the where-used list function makes it easy for us to search for any object in programs. See Figure 4-12.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig12_HTML.jpg
Figure 4-12

Hit list

To create a where-used list for methods, go to the CL_SALV_TABLE class, go to the DISPLAY method, and click on the where-used list icon (the yellow icon with a tree structure), which you can see in Figure 4-13.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig13_HTML.jpg
Figure 4-13

Where-used list icon

A dialog box will appear with the class/interface and method name with the selected option program, classes, and BSP application, as shown in Figure 4-14.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig14_HTML.jpg
Figure 4-14

Where-Used List dialog box

Click Continue. A hit list will then appear. In this example, you can see that the Display method is used in 96 programs, as shown in Figure 4-15.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig15_HTML.jpg
Figure 4-15

Hit list

To check where the method is used, double-click on an object name. This will take you to the exact location where it is used, as shown in Figure 4-16.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig16_HTML.jpg
Figure 4-16

Method display used location

Useful Buttons on the SE24 Tab

Every SE24 tab (methods, attributes, and interfaces) features buttons like Insert, Delete, Copy, Sort, Detail View, Documentation, etc. The following is a brief description of some of these buttons.
  • Insert method. The green icon shown in Figure 4-17 is used to add methods. Here, we are adding the GET_PRIMENUMBERS method to a class.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig17_HTML.jpg
    Figure 4-17

    Add Method icon

  • Delete method. The red icon shown in Figure 4-18 is used to delete a method. Here, we are deleting the GET_PRIMENUMBERS method from the column.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig18_HTML.jpg
    Figure 4-18

    Delete Method icon

Class Browser

This is used to display all the global classes and interfaces or business objects in the class library. The transaction code is CLABAP. We can do this via the Class Builder, or we can use the transaction CLABAP directly.

You’ll find a screen with the options All Classes and Business Objects, as shown in Figure 4-19.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig19_HTML.jpg
Figure 4-19

SAP easy access screen

Choose the All Classes option. You’ll get a screen showing all the standard SAP global classes and interfaces, as you can see in Figure 4-20.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig20_HTML.jpg
Figure 4-20

All classes screen

The other way to access the class browser is through the Class Builder (SE24). Go to SE24, click on Utilities, and select Class Browser. A similar dialog box of options will appear, as shown in Figure 4-21, and you can search through the classes and interfaces.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig21_HTML.jpg
Figure 4-21

Class Browser screen

Testing Classes in SE24

The Class Builder allows you to test your class and its methods without creating a test program. The test function within the class lets you specify test data and check the behavior of both static and instance methods. These may contain table parameters or not.

In this section, we look at a number of examples that will be tested via SE24.

Testing a Static Method

Let’s look at the steps needed to test a static method. To serve as the example, we will create a class called ZMY_PERCENTAGE to calculate the percentage of the two inputted numbers.

Go to SE24 and input the class name ZMY_PERCENTAGE. Click on the Create option, as shown in Figure 4-22.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig22_HTML.jpg
Figure 4-22

Inputting a class name

Choose the Class option from the dialog box that appears and then press Enter.

The dialog box shown in Figure 4-23 will appear. Input the description, select the Usual ABAP Class option, uncheck the Final checkbox, and click the Save button.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig23_HTML.jpg
Figure 4-23

Creating a class

A new screen will appear. Input the method name as CALC_PERCENT and select Static Method for the level. Set the visibility to public and enter a description, as shown in Figure 4-24.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig24_HTML.jpg
Figure 4-24

Setting the class description

Now, go to Method and select the Parameters tab. Input MYNUMBER1 and MYNUMBER2 as the importing parameters of type INTEGER and input CALC_PERCENT as an exporting parameter of type INTEGER. This is shown in Figure 4-25. Click Save.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig25_HTML.jpg
Figure 4-25

Method parameters

Go back to Method to implement source code for CALC_PERCENT. Select the method by clicking on it. You’ll get the screen shown in Figure 4-26.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig26_HTML.jpg
Figure 4-26

Source code screen

Here, we have written the logic of the program to calculate percentage. Now, check the syntax for errors using the check option (or CTRL+F2), save (CTRL+S) every element of the class, and activate the class (CTRL+F3).

To test the static method CALC_PERCENT of the ZMY_PERCENTAGE class, follow these steps:
  1. 1.
    Click the ../images/478428_1_En_4_Chapter/478428_1_En_4_Figa_HTML.jpg button. This will take you to the screen shown in Figure 4-27.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig27_HTML.jpg
    Figure 4-27

    Testing ZMY_PERCENTAGE

     
  2. 2.

    We do not need to create an instance of the object since we have to test a static method (in our case, CALC_PERCENT). Simply click the ../images/478428_1_En_4_Chapter/478428_1_En_4_Figb_HTML.jpg next to the method.

     
  3. 3.
    This will take you to the input screen of the method, where the importing parameters will be shown. Enter suitable test values for the MYNUMBER1 and MYNUMBER2 parameters and click the ../images/478428_1_En_4_Chapter/478428_1_En_4_Figc_HTML.jpg button on the toolbar. This will display the output of the returning parameter PERCENTAGE, as shown in Figure 4-28.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig28_HTML.jpg
    Figure 4-28

    Percentage output

     
The equivalent code for calling the static method created earlier is shown in the code, as follows.
PARAMETERS: p_num1 TYPE i ,
                              p_num2 TYPE i.
DATA cal_percent TYPE i.
CALL METHOD zmy_percentage=>calc_percent
  EXPORTING
    mynumber1        = p_num1
    mynumber2        = p_num2
  IMPORTING
    cal_percent = cal_percent.
WRITE: 'Percentage =', cal_percent.

In this code, we are again calculating percentage through an ABAP program. We’ve declared the P_NUM1 and P_NUM2 parameters of type string, so that we can take the input values from the selection screen. The result is stored in CAL_PERCENT, which is of the integer type.

We have now called the CALC_PERCENT method from a ZMY_PERCENTAGE class, which calculates the percentage. The importing parameters of the MYNUMBER1 and MYNUMBER2 methods are exported with P_NUM1, P_NUM2, and CAL_PERCENT stored as the result.

Testing Instance Methods

Now let’s look at a simple example of testing pertaining to instance methods.

We will create the class to calculate the cubic value of a number. In this code, we will use the constructor method to calculate the cubic value of the inputted number (importing parameter of the constructor) and set the cubic value as an attribute of the class. We will then use the Class Builder to instantiate an object of the class and calculate the cubic value. Let’s see how this is done.

Go to the SE24 Class Builder, input the object type name ZCL_MY_CUBE_CALCULATOR, and click on Create, as shown in Figure 4-29.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig29_HTML.jpg
Figure 4-29

Creating a cube class

Go to the Methods tab and input the method name CONSTRUCTOR, then press Enter. A constructor symbol is created, showing you that this is a constructor method, as you can see in Figure 4-30. For the constructor, choose a visibility of Public.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig30_HTML.jpg
Figure 4-30

Constructor method

Go to the CONSTRUCTOR method you’ve just created and click on the Specifying parameters. Input the parameter name MYNUMVALUE, type integer, as shown in Figure 4-31.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig31_HTML.jpg
Figure 4-31

Specifying parameters

Select the Attributes tab and input the attribute name MYCUBICVALUE. Set it as an instance attribute with public visibility of type integer, as shown in Figure 4-32.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig32_HTML.jpg
Figure 4-32

Attributes tab

Go back to the method constructor and double-click on it. Write the logic of the code to calculate the cube of the input number, as shown in Figure 4-33.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig33_HTML.jpg
Figure 4-33

Calculation code

Save the class and activate all the elements of the class.

Once the class is activated, we can execute the constructor method using the Test feature of the Class Builder.

From the Class Builder, click the ../images/478428_1_En_4_Chapter/478428_1_En_4_Figd_HTML.jpg button or use the F8 key. The Create Instance screen appears, as shown in Figure 4-34.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig34_HTML.jpg
Figure 4-34

Create Instance screen

Here, you will be provided space to enter value(s) for the importing parameter(s) of the constructor. In our case, there is only one parameter—MYNUMVALUE.

We will enter a value of 10 in the input field and click the ../images/478428_1_En_4_Chapter/478428_1_En_4_Fige_HTML.jpg button on the toolbar. This will create an instance of the class and will assign a value equal to the inputted number’s cube value of the MYCUBICVALUE attribute, as shown in Figure 4-35.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig35_HTML.jpg
Figure 4-35

Cubic value calculated

In short, you’ll get the cube of the number stored in the given attribute, without the need to develop a test program with relevant coding.
E

We will now see another example—a bank account demo—where will test instance methods (other than the constructor) using the functions of the Class Builder. As an example, we will use a bank account for bank customers. The class we will use is ZCL_MY_ACCOUNT_DEMO.

This class has the private instance attribute called BALANCE, based on the type BETRG. We will use this to store the customer’s account balance at any given time. This attribute is shown in Figure 4-36.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig36_HTML.jpg
Figure 4-36

Account attributes

The class also has three methods, all of which have public visibility, as shown in Figure 4-37. The methods are DEPOSIT_MONEY, WITHDRAW_MONEY, and SET_ACCOUNT_BALANCE.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig37_HTML.jpg
Figure 4-37

Class methods

The methods are defined as follows:
  • DEPOSIT_MONEY. This method adds a deposit to the current balance and returns the new one. The importing parameter is AMOUNT_DEPOSITED of type BETRG, and the returning parameter is NEW_BALANCE of type BETRG, as shown in Figure 4-38.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig38_HTML.jpg
    Figure 4-38

    Method deposit balance

  • SET_ACCOUNT_BALANCE. This method sets the balance with the new amount when money is added to the account. The importing parameter is NEW_BALANCE of type BETRG, as shown in Figure 4-39.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig39_HTML.jpg
    Figure 4-39

    NEW_BALANCE method

  • WITHDRAW_MONEY. When an amount is withdrawn from the balance, this method returns the new amount. The importing parameter is AMOUNT_WITHDRAWN of type BETRG, and the exporting parameter is NEW_BALANCE of type BETRG, as shown in Figure 4-40.

../images/478428_1_En_4_Chapter/478428_1_En_4_Fig40_HTML.jpg
Figure 4-40

Debit amount method

To test the object, activate all the elements of the global class and click on the Test button. The screen shown in Figure 4-41 appears.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig41_HTML.jpg
Figure 4-41

Testing the object

Click on SET_ACCOUNT_BALANCE and set the balance to the new balance amount, as shown in Figure 4-42.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig42_HTML.jpg
Figure 4-42

Setting the new balance

Click Execute. The account balance is now set, and you will return to the previous screen.

Go to DEPOSIT_MONEY and enter the deposit amount as 200, as shown in Figure 4-43.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig43_HTML.jpg
Figure 4-43

Setting the new balance

Click the Execute button. The output of this method is shown in Figure 4-44.

As you can see in Figure 4-44, the new balance in the account is 100,200.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig44_HTML.jpg
Figure 4-44

Setting the new balance

Let’s withdraw 99,000 from the current balance. We can see this step in Figure 4-45.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig45_HTML.jpg
Figure 4-45

Setting the new balance

As you can see in Figure 4-46, the balance remaining in the account is 1,200.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig46_HTML.jpg
Figure 4-46

Setting the new balance

Testing Methods Using Table Parameters

Up to this point, we have seen methods with parameters having single values. It is also possible to test methods that have table parameters via transaction SE24. We will demonstrate with for an example that displays an employee list from SAP standard table PA0002. The steps are as follows:
  1. 1.
    Create a global class named ZCL_EMPLOYEES, as shown in Figure 4-47.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig47_HTML.jpg
    Figure 4-47

    Global class ZCL_EMPLOYEES

     
  2. 2.
    Declare the instance attributes with visibility set to public in the Attributes tab. P_BEGDA and P_ENDDA are used to select the employee start date and end date. WA_PA0002 is a work area, and IT_PA0002 is a table. You can see this step in Figure 4-48.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig48_HTML.jpg
    Figure 4-48

    Declaring instance attributes

     
  3. 3.
    In the Methods tab, declare the instance methods as GET_EMPLOYEES and DISPLAY_EMP, with public visibility. The GET_EMPLOYEES method will list the entries in an internal table, and DISPLAY_EMP will display the employee details. You can see this step in Figure 4-49.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig49_HTML.jpg
    Figure 4-49

    Methods tab

     
  4. 4.
    To write the logic for the code, double-click on the GET_EMPLOYEES method. This will take you inside the method. Here, we select all the data from table PA0002 and insert it into table IT_PA0002. You can see this step in Figure 4-50.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig50_HTML.jpg
    Figure 4-50

    Writing the code

     
  5. 5.
    In the DISPLAY_EMP method, use the LOOP AT statement to display all the records, as shown in Figure 4-51.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig51_HTML.jpg
    Figure 4-51

    Using the LOOP AT statement

     
  6. 6.

    Activate all the elements of the global class and test the class.

     
  7. 7.
    On the screen, you need to input the employee start date and end date, as you can see in Figure 4-52.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig52_HTML.jpg
    Figure 4-52

    Selection screen

     
  8. 8.
    When you click the Execute button from the GET_EMPLOYEES method, it will give you a number of entries in the internal table IT_PA0002, as shown in Figure 4-53.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig53_HTML.jpg
    Figure 4-53

    Internal table

     
  9. 9.
    When you execute the DISPLAY_EMP method, it will display all the records, as shown in Figure 4-54.
    ../images/478428_1_En_4_Chapter/478428_1_En_4_Fig54_HTML.jpg
    Figure 4-54

    Record display

     
Equivalent code to call this method from a program is shown here:
REPORT  ZCL_EMPLOYEES.
PARAMETERS: p_begda TYPE begda,
            p_endda TYPE endda.
DATA: obj_emp TYPE REF TO ZCL_EMPLOYEES.
CREATE OBJECT obj_emp.
CALL METHOD obj_emp->get_employees.
CALL METHOD obj_emp->display_emp.
The output of this code is shown in Figure 4-55.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig55_HTML.jpg
Figure 4-55

Output

Here, we want to fetch all the employee records from table PA0002. We have declared the parameters P_BEGDA and P_ENDDA. Data OBJ_EMP refers to class ZCL_EMPLOYEES. Now, we create an object called OBJ_EMP, which has instance attributes, and we call the GET_EMPLOYEES and DISPLAY_EMP methods to get the required output, which is shown in Figure 4-56.
../images/478428_1_En_4_Chapter/478428_1_En_4_Fig56_HTML.jpg
Figure 4-56

Output

Summary

In this chapter, we saw how we can use the Object Navigator to create global classes. We then explored the various tabs of the Class Builder, particularly the Methods tab. We discussed the Class Browser, and other useful options of the transaction SE24. Finally, we saw a number of examples using the transaction SE24 that could be tested using the transaction itself rather than having to create a program for them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.64