Skip to Content

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. image

    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

    image

    Figure 2: Methods of class  ZCL_VEHICLE  after import

    image

    Figure 3: Global typings in ZCL_VEHICLE class

    image

    Figure 4: Methods of class  ZCL_PLANE  and class hierarchy

    image

    Figure 5: Methods of class  ZCL_SHIP  and class hierarchy

    image

    Figure 6: Attributes of class  ZCL_SHIP and class hierarchy

    image

    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:)

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

  1. Matthew Billingham
    >I especially found it difficult when
    >implementing a method, where there is no
    >insight into class’s attributes at the same time.

    If you click on “Display Object List” (Ctrl-Shift-F5) from within SE24, you can see precisely the class attributes and methods, and the heirarchy of sub/super classes and interfaces.  Further, you can drag and drop the attributes and methods into the editor.

    Doesn’t this address the problems you perceive with SE24?

    (0) 
    1. Marcin Pciak Post author
      You are right Matthew – as far as the visibility and hierarchy is concerned, however attributes’ typing is then still missing which is often essential to their correct usage.
      (0) 

Leave a Reply