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
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.
- 1.
To create a new global class, use the context menu in the navigation area.
- 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.
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.
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.
These methods can be called from any ABAP program.
Class Builder Tabs
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.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.
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.
Useful Buttons on the SE24 Tab
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.
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.
Choose the Class option from the dialog box that appears and then press Enter.
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).
- 1.Click the button. This will take you to the screen shown in Figure 4-27.
- 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 next to the method.
- 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 button on the toolbar. This will display the output of the returning parameter PERCENTAGE, as shown in Figure 4-28.
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.
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.
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 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.
- 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.
- 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.
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.
Click Execute. The account balance is now set, and you will return to the previous screen.
Click the Execute button. The output of this method is shown in Figure 4-44.
Testing Methods Using Table Parameters
- 1.Create a global class named ZCL_EMPLOYEES, as shown in Figure 4-47.
- 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.
- 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.
- 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.
- 5.In the DISPLAY_EMP method, use the LOOP AT statement to display all the records, as shown in Figure 4-51.
- 6.
Activate all the elements of the global class and test the class.
- 7.On the screen, you need to input the employee start date and end date, as you can see in Figure 4-52.
- 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.
- 9.When you execute the DISPLAY_EMP method, it will display all the records, as shown in Figure 4-54.
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.