Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
MarcinPciak
Active Contributor

Dear Reader,

This is my first blog ever so please be tolerant. I hope you will find it useful.

Class Builder, though powerful tool, seems somehow complex to me. Switching between different tab pages and looking for particular option sometimes is not so intuitive as it should be. I especially found it difficult when implementing a method, where there is no insight into class's attributes at the same time. Furthermore if our object model is more sophisticated and consits of entire class hieraries, it might become a nightmare.
I think we will all agree that defining local classes (together with their hierarchies) in a program requires much less efford as we have everything on one "board".

Fortunately SAP, together with Class Builder, provides fancy tool allowing developer to derive global class from the local one (even with its all subsequent classes). For this following two steps are required:

  • Creating local object model within temporary program (together with inheritance if necessary).
  • Use Class Builder to produce global classes as defined in temporary program.

Creating local object model

Lets consider such program:

  1. First we define and implement local class lcl_vehicle.

  2. *&---------------------------------------------------------------------*
    *& Report  Z_LCL_TO_CL
    *&
    *&---------------------------------------------------------------------*
    *& Local class to global one
    *&---------------------------------------------------------------------*

    REPORT  z_lcl_to_cl.

    *----------------------------------------------------------------------*
    *       CLASS lcl_vehicle DEFINITION
    *----------------------------------------------------------------------*
    CLASS lcl_vehicle DEFINITION.
      PUBLIC SECTION.
        TYPES: my_i_type TYPE i.

        METHODS: accelerate,
                 status.

      PROTECTED SECTION.
        DATA: speed TYPE my_i_type.

    ENDCLASS.                    "vehicle DEFINITION

    *----------------------------------------------------------------------*
    *       CLASS lcl_vehicle IMPLEMENTATION
    *----------------------------------------------------------------------*
    CLASS lcl_vehicle IMPLEMENTATION.
      METHOD accelerate.
        speed = speed + 1.
      ENDMETHOD.                    "accelerate

      METHOD status.
        WRITE: / 'Speed:', speed.
      ENDMETHOD.                    "status
    ENDCLASS.                    "vehicle IMPLEMENTATION

  3. Secondly we define and implement local class lcl_plane inheriting from the above. Method status has been redefined in order to achieve specialization.
  4. *----------------------------------------------------------------------*
    *       CLASS lcl_plane DEFINITION
    *----------------------------------------------------------------------*
    CLASS lcl_plane DEFINITION INHERITING FROM lcl_vehicle.
      PUBLIC SECTION.
        METHODS: rise,
                 status REDEFINITION.

      PRIVATE SECTION.
        DATA meters TYPE p.

    ENDCLASS.                    "plane DEFINITION

    *----------------------------------------------------------------------*
    *       CLASS lcl_plane IMPLEMENTATION
    *----------------------------------------------------------------------*
    CLASS lcl_plane IMPLEMENTATION.
      METHOD rise.
        meters = meters + '12.5'.
      ENDMETHOD.                    "rise

      METHOD status.
        ULINE.
        WRITE: / 'Plane', / , 'Altitude:', meters.
        super->status( ).
      ENDMETHOD.                    "status
    ENDCLASS.                    "plane IMPLEMENTATION

  5. Then we define and implement local class lcl_ship inheriting from lcl_vehicle. As in previous class, status method is redefined here too.
  6. *----------------------------------------------------------------------*
    *       CLASS lcl_ship DEFINITION
    *----------------------------------------------------------------------*
    CLASS lcl_ship DEFINITION INHERITING FROM lcl_vehicle.
      PUBLIC SECTION.
        METHODS: sail,
                 status REDEFINITION.

      PRIVATE SECTION.
        DATA miles TYPE p DECIMALS 2.
    ENDCLASS.                    "ship DEFINITION

    *----------------------------------------------------------------------*
    *       CLASS lcl_ship IMPLEMENTATION
    *----------------------------------------------------------------------*
    CLASS lcl_ship IMPLEMENTATION.
      METHOD sail.
        miles = miles + '0.03'.
      ENDMETHOD.                    "sail

      METHOD status.
        ULINE.
        WRITE: / 'Ship', / , 'Miles:', miles.
        super->status( ).
      ENDMETHOD.                    "status
    ENDCLASS.                    "ship IMPLEMENTATION


  7. Finally we use our classes in the program
  8. "start of the program
    START-OF-SELECTION.

      DATA: r_plane TYPE REF TO lcl_plane,
            r_ship  TYPE REF TO lcl_ship.

      CREATE OBJECT: r_plane, r_ship.

      DO 40 TIMES.
        r_plane->accelerate( ).
        r_plane->rise( ).
        r_ship->accelerate( ).
        r_ship->sail( ).
      ENDDO.

      r_plane->status( ).
      r_ship->status( ).

The object model used here is very simple, but this is not important here. We only needed some model which will use inheritance together with method redefinition, to show that the conversion to global model takes generalization/specialization relationship into account.


Creating global class form local one
  1. Go to SE24 (Class Buider)
  2. From Menu Bar choose Object type->Import->Local classes in program. Following dialog box will appear:

  3. Figure 1: Importing classes from program
  4. In Program name provide your temporary program name and press Enter key or Expand button next to the input field.
  5. If classes are defined inside the program’s include, check Explode INCLUDEs box.
  6. Select classes you want to derive new global ones from.
  7. Remember to use customer namespace for classes (at least names should start with Z or Y ).
  8. Click Import button to create global classes.
  9. Assign package and transport request as usually.
  10. Now display the classes and notice that besides the attributes, global typings and methods, whole object model hierarchy was kept.

  11. Hint!
    In order to see components of classes grouped according to superclass, go to Utilities->Settings->Class Builder->check Group by Interfaces and Superclasses

    Figure 2: Methods of class  ZCL_VEHICLE  after import
    Figure 3: Global typings in ZCL_VEHICLE class
    Figure 4: Methods of class  ZCL_PLANE  and class hierarchy
    Figure 5: Methods of class  ZCL_SHIP  and class hierarchy
    Figure 6: Attributes of class  ZCL_SHIP and class hierarchy
    Figure 7: Superclass of ZCL_SHIP class

Disadvantages

  • As you probably have noticed, no semantic data are available for class’s components. Even if you use DDIC typing of the attributes it will not take effect here. So this must be maintained manually.
  • Local typings from program must be copied manually (applies to local typing in classes achieved by Local types button on application toolbar).

Except those two above disadvantages, I didn’t find any. This approach makes whole OO modeling process easier.

Enjoy…SAP:)

4 Comments