Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to approach ABAP OO programming the right way...

aris_hidalgo
Contributor
0 Kudos

Hello experts,

I am an old school procedural ABAP programmer and I recently I have been experementing with ABAP Objects since I've read a few columns the advantages of ABAP OO and also currently learning its syntax. Now, does ABAP Objects conform to the old way of say, doing reports like at selection-screen output, at selection-screen, on value-request for..., start-of-selection, end-of-selection, top-of-page, etc. I have been doing some practice programs and I am not sure if my approach is correct. Example, I created a class named cl1 and I have a method named upload. Now, the UPLOAD method contains the function 'GUI_UPLOAD'. Is this the right way of doing it? Also, How come I cannot create structures inside a class?

Again, thanks guys and have a nice day!

1 ACCEPTED SOLUTION
6 REPLIES 6

RichHeilman
Developer Advocate
Developer Advocate
0 Kudos

Hi. First, If you are writing a report program, it is better to stay with the event driven model. You can put all of your code with in the events inside a local class/method. There is nothing wrong with that.

Also, you can do as you are doing with the GUI_UPLOAD function module, but there is a standard class/method to handle this.

CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD. It would be better to use that.

Also, you can create structures within your class.

class lcl_app definition.

public section.

Types: begin of ttype,
       fld1(10) type c,
       fld2(10) type c,
       end of ttype.

data: itab type table of ttype.
data: wa type ttype.


endclass.

Regards,

Rich Heilman

Former Member
0 Kudos

Hi Viraylab,

Your class 'cl1' contains a method UPLOAD which inturn invokes GUI_UPLOAD.

This is perfectly fine, syntactically.

And according to OOP concepts, it is perfectly

fine if your UPLOAD method of class 'cl1' does

something more than just calling GUI_UPLOAD.

On the contrary, if your UPLOAD method doesnot

have any additional functionality and is just

calling GUI_UPLOAD, then it doesn't make any

sense to have that UPLOAD method in your class

'cl1'.

Instead, you can directly use-

CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD.

Giving another scenario-

If you have a method say 'm1' of class 'C1'

and this method does nothing but invoking

another method 'meth' of another class 'C2',

then you can think of having this method 'm1'

if it would reduce the number of parameters

being passed.

(ie, the number of parameters method 'm1'

takes is drastically less than the number

of parameters method 'meth' takes, in which

case the other parameters that have to be

passed to 'meth' would be decided within the

method 'm1' itself).

Well, there are many Design Issues like this

and we need to keep in mind many things inorder

to have a proper design.

You can create structures inside a class.

Eg:

class cl1 definition.

public section.

Types:

begin of t1,

f1 type numc2,

end of t1.

data:

struc1 type t1.

endclass.

If you are new to OO ABAP then you can refer to the following links-

http://www.sapgenie.com/abap/OO/

http://www.sapgenie.com/abap/OO/index.htm

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b5654f411d194a60000e8353423/content.htm

http://www.esnips.com/doc/375fff1b-5a62-444d-8ec1-55508c308b17/prefinalppt.ppt

Let me know if you have any further queries regarding this.

Mark useful answers.

Regards,

Tanuja.

Message was edited by: Tanuja Sarraju

Message was edited by: Tanuja Sarraju

Message was edited by: Tanuja Sarraju

nishanthbhandar
Contributor
0 Kudos

The best way to start off OOPs approach is to start with the local classes.Create some local classes in a sample program and implement them.Understand the use of public,private and protected methods.Once you are familiar with this try creating a global class in SE24.Become familiar with this transaction.You can create some methods here with some simple functionalities and then use them in your sample program.

Also for your reference try the sample programs available in ABAPDOCU transaction.Also for example for the class that is used for GUI_DOWNLOAD(CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD).

Try and undertstand how it's done in the standard program and replicate it.

Former Member
0 Kudos

Hi,

I have these three progs from one of the previous posts.

Good approach on using constructors. I am not finding the original link.


*&---------------------------------------------------------------------*
*& Report ZABAP_OBJ_01 *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT zabap_obj_01 .

PARAMETERS : p_vbeln LIKE vbap-vbeln OBLIGATORY,
             p_matnr LIKE mara-matnr.


TYPES : BEGIN OF ty_vbap,
vbeln TYPE vbap-vbeln,
matnr TYPE vbap-matnr,
arktx TYPE vbap-arktx,
END OF ty_vbap.

*---------------------------------------------------------------------*
* CLASS sales_order DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS sales_order DEFINITION.
  PUBLIC SECTION.
    DATA : v_matnr TYPE mara-matnr,
    v_vbeln TYPE vbap-vbeln.

    METHODS : constructor IMPORTING vbeln TYPE vbap-vbeln
    matnr TYPE mara-matnr OPTIONAL.
    DATA : it_vbap TYPE STANDARD TABLE OF ty_vbap.
    METHODS : get_vbap_details,
    disp_vbap_details.

ENDCLASS. "sales_order DEFINITION

*---------------------------------------------------------------------*
* CLASS sales_order IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS sales_order IMPLEMENTATION.
  METHOD get_vbap_details.
    CLEAR : it_vbap.
    REFRESH : it_vbap.
    SELECT vbeln
    matnr
    arktx
    FROM vbap
    INTO TABLE it_vbap
    WHERE vbeln = p_vbeln.
  ENDMETHOD. "get_vbap_details

  METHOD constructor.
    CLEAR : v_vbeln,
    v_matnr.
    v_vbeln = vbeln.
    v_matnr = matnr.
  ENDMETHOD. "constructor

  METHOD disp_vbap_details.
    DATA : wx_vbap LIKE LINE OF it_vbap.
    LOOP AT it_vbap INTO wx_vbap.
      WRITE 😕 wx_vbap-vbeln,
      wx_vbap-matnr,
      wx_vbap-arktx.
    ENDLOOP.
    CLEAR : it_vbap.
  ENDMETHOD. "disp_vbap_details
ENDCLASS. "sales_order IMPLEMENTATION
DATA : obj TYPE REF TO sales_order.

START-OF-SELECTION.

  IF NOT p_matnr IS INITIAL.
    CREATE OBJECT obj EXPORTING vbeln = p_vbeln
    matnr = p_matnr.
  ELSE.
    CREATE OBJECT obj EXPORTING vbeln = p_vbeln.
  ENDIF.
  CALL METHOD obj->get_vbap_details.
  CALL METHOD obj->disp_vbap_details.

*&---------------------------------------------------------------------*
*& report ziga_abapobjects_asgn01 *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

  REPORT ziga_abapobjects_asgn01 .

  PARAMETER : p_matnr LIKE mara-matnr.

*---------------------------------------------------------------------*
* CLASS lcl_material DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_material DEFINITION.
  PUBLIC SECTION.
    DATA: v_matnr TYPE mara-matnr.
    METHODS : constructor IMPORTING matnr TYPE mara-matnr,
    get_material_description.

  PRIVATE SECTION.
    DATA : v_maktx TYPE makt-maktx.
ENDCLASS. "lcl_material DEFINITION

*---------------------------------------------------------------------*
* CLASS lcl_material IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_material IMPLEMENTATION.
  METHOD get_material_description.
    CLEAR v_maktx.
    SELECT SINGLE maktx INTO v_maktx
    FROM makt
    WHERE matnr = v_matnr AND
    spras = 'E'.
  ENDMETHOD. "get_material_description

  METHOD constructor.
    CLEAR v_matnr.
    v_matnr = matnr.
  ENDMETHOD. "constructor
ENDCLASS. "lcl_material IMPLEMENTATION


DATA : obj TYPE REF TO lcl_material.

START-OF-SELECTION.

  CREATE OBJECT obj EXPORTING matnr = p_matnr.

  CALL METHOD obj->get_material_description.


  prg3)
  report ziga_abapobjects_asgn01 .

  PARAMETER : p_matnr LIKE mara-matnr.

*---------------------------------------------------------------------*
* CLASS lcl_material DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_material DEFINITION.
  PUBLIC SECTION.
    METHODS : constructor IMPORTING matnr TYPE mara-matnr,
    write_material_desc.
    CLASS-METHODS : class_constructor.

  PRIVATE SECTION.
    CLASS-DATA: v_matnr TYPE mara-matnr.
    DATA : v_maktx TYPE makt-maktx.
    METHODS : get_material_description.
ENDCLASS. "lcl_material DEFINITION

*---------------------------------------------------------------------*
* CLASS lcl_material IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_material IMPLEMENTATION.
  METHOD get_material_description.
    CLEAR v_maktx.
    SELECT SINGLE maktx INTO v_maktx
    FROM makt
    WHERE matnr = v_matnr AND
    spras = 'E'.
  ENDMETHOD. "get_material_description

  METHOD constructor.
    WRITE 😕 'Inside Instance Constructor'.
    CLEAR v_matnr.
    v_matnr = matnr.
    CALL METHOD get_material_description.
  ENDMETHOD. "constructor

  METHOD write_material_desc.
    WRITE 😕 'Material Description :', v_maktx.
  ENDMETHOD.                    "write_material_desc

  METHOD class_constructor.
    WRITE 😕 'Inside Static Constructor'.
  ENDMETHOD.                    "class_constructor
ENDCLASS. "lcl_material IMPLEMENTATION


DATA : obj TYPE REF TO lcl_material,
obj1 TYPE REF TO lcl_material.

START-OF-SELECTION.

  CREATE OBJECT obj EXPORTING matnr = p_matnr.

  CALL METHOD obj->write_material_desc.

  CREATE OBJECT obj1 EXPORTING matnr = '000000000000000110'.

  CALL METHOD obj1->write_material_desc.

  CALL METHOD obj->write_material_desc.

  prg4)
  report ziga_abapobjects_asgn01 .

  PARAMETER : p_matnr LIKE mara-matnr.

*---------------------------------------------------------------------*
* CLASS lcl_material DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_material DEFINITION.
  PUBLIC SECTION.
    METHODS : constructor IMPORTING matnr TYPE mara-matnr,
    write_material_desc,
    get_material_description.
    CLASS-METHODS : class_constructor.

  PRIVATE SECTION.
    CLASS-DATA: v_matnr TYPE mara-matnr.
    DATA : v_maktx TYPE makt-maktx.

ENDCLASS. "lcl_material DEFINITION

*---------------------------------------------------------------------*
* CLASS lcl_material IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_material IMPLEMENTATION.
  METHOD get_material_description.
    CLEAR v_maktx.
    SELECT SINGLE maktx INTO v_maktx
    FROM makt
    WHERE matnr = v_matnr AND
    spras = 'E'.
  ENDMETHOD. "get_material_description

  METHOD constructor.
    WRITE 😕 'Inside Instance Constructor'.
    CLEAR v_matnr.
    v_matnr = matnr.
  ENDMETHOD. "constructor

  METHOD write_material_desc.
    WRITE 😕 'Material Description :', v_maktx.
  ENDMETHOD.                    "write_material_desc

  METHOD class_constructor.
    WRITE 😕 'Inside Static Constructor'.
  ENDMETHOD.                    "class_constructor
ENDCLASS. "lcl_material IMPLEMENTATION


DATA : obj TYPE REF TO lcl_material,
obj1 TYPE REF TO lcl_material.

START-OF-SELECTION.

  CREATE OBJECT obj EXPORTING matnr = p_matnr.

  CALL METHOD obj->get_material_description.
  CALL METHOD obj->write_material_desc.

  CREATE OBJECT obj1 EXPORTING matnr = '000000000000000110'.

  CALL METHOD obj1->get_material_description.
  CALL METHOD obj1->write_material_desc.

  CALL METHOD obj->get_material_description.
  CALL METHOD obj->write_material_desc.


  REPORT ziga_abapobjects_asgn01 .

  PARAMETER : p_matnr LIKE mara-matnr.

*---------------------------------------------------------------------*
* CLASS lcl_material DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_material DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS : write_material_desc,
    get_material_description,
    class_constructor.

  PRIVATE SECTION.
    CLASS-DATA: v_matnr TYPE mara-matnr.
    CLASS-DATA: v_maktx TYPE makt-maktx.

ENDCLASS. "lcl_material DEFINITION

*---------------------------------------------------------------------*
* CLASS lcl_material IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*
CLASS lcl_material IMPLEMENTATION.
  METHOD get_material_description.
    CLEAR v_maktx.
    SELECT SINGLE maktx INTO v_maktx
    FROM makt
    WHERE matnr = v_matnr AND
    spras = 'E'.
  ENDMETHOD. "get_material_description

  METHOD write_material_desc.
    WRITE 😕 'Material Description :', v_maktx.
  ENDMETHOD.                    "write_material_desc

  METHOD class_constructor.
    WRITE 😕 'Inside Static Constructor'.
    v_matnr = '000000000000000110'.
  ENDMETHOD.                    "class_constructor
ENDCLASS. "lcl_material IMPLEMENTATION

START-OF-SELECTION.

  CALL METHOD lcl_material=>get_material_description.
  CALL METHOD lcl_material=>write_material_desc.



Arun Sambargi.

Former Member
0 Kudos

Hi Viraylab,

Welcome to the OOP ( object-oriented-programming ), you did not get back after your first post , but anyhow I have only one sentence to say, Please first try to get an idea of OOP rather starting OOP- Language i.e. ABAP OO.

As your post also says, I mean, implementing classes , methods or learning ABAP OO syntax is not a deal, but where, how, and when to use CLASSES and methods is a deal

Hope you get what I try to say,

Best Regards, and Good Luck !.

Shah H.