Wednesday, 11 October 2017

ABAP: Class Builder

Classes are the enhanced version of function modules. ABAP classes support object oriented principles.

ABAP GUI provides 'Class Builder' user interface to build the classes. Use the transaction code 'SE24' to open the 'Class Builder' user interface.

Below step-by-step procedure explains how to define class.

Step 1: Use the transaction ‘SE24’ to open class builder.


Step 2: Give the class name as ZCL_EMPLOYEE. ‘CL’ stands for class, it is just a naming convention.
Click on ‘Create’ button.

It opens below window.

Give the description as ‘Model class to represent employee information’.
Click on ‘Save’ button.

It opens below window.
Click on the button ‘Local Object’.

It opens below window.
Activate the class ZCL_EMPLOYEE.

It opens below window.
Select all the object and activate them.

Step 3: Define constructor to the class ZCL_EMPLOYEE.

Click on the tab ‘Methods’, give the Mehtod name as ‘CONSTRUCTOR’ and press Enter. Change the access from private to public.
Next click on the tab ‘parameters’.

Define the parameters ID, FIRST_NAME, and LAST_NAME.
Save the changes.

Now click on the tab ‘METHODS’. Double click on the method CONSTRUCTOR.
After double click, it opens below window.
Update the constructor definition like below.

  method CONSTRUCTOR.
    _id = ID.
    _first_name = FIRST_NAME.
    _last_name = LAST_NAME.
  endmethod.
Save the source code.

Go back and click on ‘Attributes’ tab.
Define the attributes _id, _first_name and _last_name.

Set the Level as ‘Instance Attribute’, Visibility to Private and Type to STRING.
Save the program and activate the application.

It opens below window, select all the press Ok button.
Click on the button ‘Source Code-Based’
You can able to see the source code of ZCL_EMPLOYEE.
Below one is the source code.

Z_CL_EMPLOYEE
class ZCL_EMPLOYEE definition
  public
  final
  create public .

public section.

  methods CONSTRUCTOR
    importing
      !ID type STRING
      !FIRST_NAME type STRING
      !LAST_NAME type STRING .
protected section.
private section.

  data _ID type STRING .
  data _FIRST_NAME type STRING .
  data _LAST_NAME type STRING .
ENDCLASS.



CLASS ZCL_EMPLOYEE IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_EMPLOYEE->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* | [--->] ID                             TYPE        STRING
* | [--->] FIRST_NAME                     TYPE        STRING
* | [--->] LAST_NAME                      TYPE        STRING
* +--------------------------------------------------------------------------------------</SIGNATURE>
  method CONSTRUCTOR.
    _id = ID.
    _first_name = FIRST_NAME.
    _last_name = LAST_NAME.
  endmethod.
ENDCLASS.



Previous                                                 Next                                                 Home

No comments:

Post a Comment