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:
Lets consider such program:
*&---------------------------------------------------------------------*
*& 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
*----------------------------------------------------------------------*
* 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
*----------------------------------------------------------------------*
* 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
"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.
Hint!
In order to see components of classes grouped according to superclass, go to Utilities->Settings->Class Builder->check Group by Interfaces and Superclasses
Disadvantages
Except those two above disadvantages, I didn’t find any. This approach makes whole OO modeling process easier.
Enjoy…SAP:)