340 likes | 524 Views
Class Builder Tutorial. Presented By- Amit Singh & Sylendra Prasad. How to work with Class Builder (SE24). Goto transaction SE24. Give a name and press create. A pop up window appears asking for selecting class or interface. Select class and press continue button,.
E N D
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad
How to work with Class Builder (SE24) • Goto transaction SE24.
Give a name and press create. A pop up window appears asking for selecting class or interface. • Select class and press continue button,
Now another pop up comes asking for short description , instantiation ( like Public, Private, Abstract and Protected ) and type of the class (normal ABAP Class, Persistent Class or Exception class) . Select the appropriate class type. • If you don’t want to create any subclass of this class , then check the checkbox named “Final”.
Now you will get the class builder screen. • There are a lot of options like Methods, Attributes, Events, friends, Interfaces,Constructor,Class Constructor etc.
Defining attributes • Goto attributes tab. Specify the attribute name , level ( Instance or Static) , visibility, associated type, description , initial value etc. • Usually attributes are defined in the private section of the class so that only methods of the class can manipulate them.
Defining methods • Click on methods tab. • Give a method name, level (Instance or Static), visibility (Public , protected or private ) and description.
If you are using parameters to import or export data from main program, click on parameters button and specify the appropriate parameters. • Constructor is a special method which is automatically called whenever an instance of the class is created. It can be added to the set of methods by clicking on the Constructor button.
Class Constructor is the static version of Constructor and can be included in the methods list by clicking “Class Constructor” in the toolbar.
Implementing the methods • Double click on any method name and you will get an editor to perform the coding. • Provide the coding within METHOD….ENDMETHOD.
Creating interfaces • Goto transaction SE24. • Give name and click on create and select interface radio button and give a short description. • As in the class , you can define methods, attributes, events, interfaces within interface. • After activating the interface, you can link the methods in the class by giving the interface name in the Interfaces tab.
On saving, automatically all the methods in the interface will be added to the Methods tab in the class. Provide the coding for the methods as explained previously.
Defining Events • In the events tab, give the event description like name , level (Instance or Static), Visibility (public, protected, private) and short description. • Events can be handled by methods either in the same class or in a different class.
Handling events in the same class • Define a method in the Methods tab and activate the class now. • Click on the detailed view icon . • Check the “Event handler for “ checkbox and fill the class and the required event. • Now a link icon appears in “Method type” column like this.
If in the event definition, any parameters are defined, they will have to be copied to the parameter interface of the event handler method. • So go to the Parameter interface of the method by clicking on Parameter button. • Copy the parameters of the event by clicking on the following icon.
Linking of Event and the method can be made complete by using the statement SET HANDLER. • User defined events are raised by using the statement RAISE EVENT <event_name>. • In the above example, while processing HANDLE_DOUBLE_CLICK , we raise the event my_event and it will be handled by the method handle_user_def.
Handling events of standard class • Suppose you want to handle double click event in ALV grid, you can create a custom class and create a method to handle the event. • Click on the detailed view icon . • Check the “Event handler for “ checkbox and fill the class and the required event.
Now a link icon appears in “Method type” column like this. • Now you can provide the implementation of the event handler method by double clicking the method name.
Defining Friend classes • If a class wants to access the protected and private components of another class, then the former can be made a “friend” of the latter. • Goto Friends tab in Class Builder and specify the friend class. • Let us create a private attribute for the class so that it can be accessed by friend function.
Now create a friend class which has a method to access the private attribute of the previous class. • Now you can provide the implementation of the method showing the way in which the private attribute is accessed.
How to create sub classes (Inheritance) • In properties of the class, specify the super class. • On saving the subclass, the public and protected components of the super class will automatically be added to the corresponding list.
Redefining superclass methods • Place the cursor on the method to be redefined and click on the Redefine Icon • You can redefine the default superclass implementation in the editor like this. • In the above code, CALL METHOD super->handle_double_click is used to execute the super class implementation, before performing subclass implementation.
In the main program, the subclass objects are instantiated and redefined methods can be called. • When the above program is executed, initially control goes to subclass’s redefined method handle_double_click. Then the superclass implementation is done and control comes back to perform subclass implementation.
Creating persistent classes • Global classes, as such are persistent, but their instances need not be. Usually the data, after program execution, is lost. • If the data is to be stored permanently ( persistently) , they need to be stored in a database. Persistent object concept was introduced with this idea in mind. • To create persistent objects, Go to SE24 and select persistent class. • The class name should start with ZCL_( for eg: ZCL_AMIT_PERST)
Now automatically, various components are generated in Methods ( like handle_exception, get, init, set, invalidate) , events , friends tab.
In addition, a class agent starting with ZCA_ ( ZCA_AMIT_PERST) and a base class ( ZCB_AMIT_PERST), which is the super class of agent class, are also generated. This agent class not only manages the instances of persistent classes, but also performs the actual database accesses. • The base class (ZCB_AMIT_PERST) can also be viewed in the friends tab of the created class. • Now you can map the database to the created class by clicking on the ‘Persistence’ button in the toolbar. • A pop up appears asking for database table/structure
Now you will get all the table fields in the bottom-most section. • Now you can double click on any of the table fields. You can edit the field properties and now when you click on ‘up arrow’ close to the description to update it in the persistent class.
When you click Save and click on ‘Back’ button, new GET_<field_name> ( for read-only fields ) and SET_<field_name> ( for other fields ) are generated in the Methods tab. • Now activate the class and the agent class.
Now in the main program, you need to define a reference variable of the agent class, filling it with reference from the static attribute AGENT. • Now create the new instance of persistence class using the method of base class CREATE_PERSISTENT. If an object already exists, you can raise the exception CX_OS_OBJECT_EXISTING by using try catch statements. • Fields which are not read-only can be set by using SET_<field_name> statements. • Now the updates can be made permanent by using COMMIT WORK statement. • After creation of persistent objects, they can be loaded into main program using the method GET_PERSISTENT passing the key values in the parameter interface. • Data from specific fields can be obtained by the statement GET_<field_name>.
Inheriting Standard SAP Classes • Standard classes can be extended by creating their subclasses.