Use inheritance to extend local classes

Use inheritance to extend local classes

If you are using ABAP Objects and writing local classes in your programs you can extend their functionality by using inheritance and global classes. This provides benefits in two ways: 1) re-use of tested and implemented code, and 2) not having to instantiate multiple objects.

For example, rather than writing methods in your class to do local file operations using FM WS_DOWLOAD or others like it have the local class inherit from global class CL_GUI_FRONTEND_SERVICES. Now your local class has all the functionality needed to do file upload and download, and more. And, you only have to instantiate one object.

How: the INHERETING FROM addition to the local class definition can point to either a global class in the repository or to another local class previously defined in your program.

I know for sure this works in 4.6x. OO was introduced in pieces starting in 4.0 with new stuff added with every release all the way up to 4.6c. So, I can also say for sure that it won't work prior to version 4.0x.


PROGRAM Z_CLASS_INHERIT_DEMO

* Class LOCAL_APPLICATION definition

CLASS local_application DEFINITION
      INHERITING FROM cl_gui_frontend_services.

  PUBLIC SECTION.

    METHODS:

      constructor
        IMPORTING i_file TYPE text200,

...

  PRIVATE SECTION.

    DATA: output_file TYPE string.

...

ENDCLASS.

...

* Global data delcarations

DATA: lcl TYPE REF TO local_application.
PARAMETERS: p_file TYPE text200.
...

START-OF-SELECTION.

CREATE

    Requires Free Membership to View

    When you register, you will start receiving targeted emails from my award-winning team of editorial writers. Our goal is to keep you informed on the hottest topics and biggest challenges faced by SAP professionals today.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchSAP.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSAP.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

OBJECT lcl TYPE local_application EXPORTING i_file = p_file. ... END-OF-SELECTION. * Call the download method * inherited from global class * cl_gui_frontend_services CALL METHOD lcl->gui_download EXPORTING filename = output_file * FILETYPE = 'ASC' CHANGING data_tab = itab EXCEPTIONS file_write_error = 1 no_batch = 2 gui_refuse_filetransfer = 3 invalid_type = 4 no_authority = 5 unknown_error = 6 header_not_allowed = 7 separator_not_allowed = 8 filesize_not_allowed = 9 header_too_long = 10 dp_error_create = 11 dp_error_send = 12 dp_error_write = 13 unknown_dp_error = 14 access_denied = 15 dp_out_of_memory = 16 disk_full = 17 dp_timeout = 18 file_not_found = 19 dataprovider_exception = 20 control_flush_error = 21 OTHERS = 22.

This was first published in December 2002

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.

    Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.