Application Development and Automation 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: 
Read only

question on Method interface

prabhu_s2
Active Contributor
0 Likes
578

Hi

I've the following method def:

   METHODS : METH1 IMPORTING INPUT1 TYPE I   
                                                              INPUT2 TYPE REF TO  CL_1.

where CL_1 is the class withing where the method is defined.

Question is, what is this kind of definition and whats the use in real time implementation?

thkx

P.S

1 ACCEPTED SOLUTION
Read only

naimesh_patel
Active Contributor
0 Likes
536

This type of definition is used when you want to access other related object's attribute in your current method.

Check this code:


*----------------------------------------------------------------------*
*       CLASS lcl_car DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_car DEFINITION.

  PUBLIC SECTION.
    DATA: w_name TYPE string.

    METHODS:
      write_name
        IMPORTING
          io_obj TYPE REF TO lcl_car.

ENDCLASS.                    "lcl_car DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_car IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_car IMPLEMENTATION.

  METHOD write_name.

    WRITE: / 'Own attribute', me->w_name.

    WRITE: / 'other object', io_obj->w_name.

  ENDMETHOD.                    "write_name

ENDCLASS.                    "lcl_car IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS lcl_bmw DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_bmw DEFINITION INHERITING FROM lcl_car.

ENDCLASS.                    "lcl_bmw DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_merc DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_merc DEFINITION INHERITING FROM lcl_car.

ENDCLASS.                    "lcl_merc DEFINITION


START-OF-SELECTION.
  DATA: lo_bmw   TYPE REF TO lcl_bmw,
        lo_merc  TYPE REF TO lcl_merc.

  CREATE OBJECT: lo_bmw,
                 lo_merc.

  lo_bmw->w_name  = 'BMW'.
  lo_merc->w_name = 'Merc'.

  lo_bmw->write_name( lo_merc ).

Regards,

Naimesh Patel

3 REPLIES 3
Read only

naimesh_patel
Active Contributor
0 Likes
537

This type of definition is used when you want to access other related object's attribute in your current method.

Check this code:


*----------------------------------------------------------------------*
*       CLASS lcl_car DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_car DEFINITION.

  PUBLIC SECTION.
    DATA: w_name TYPE string.

    METHODS:
      write_name
        IMPORTING
          io_obj TYPE REF TO lcl_car.

ENDCLASS.                    "lcl_car DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_car IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_car IMPLEMENTATION.

  METHOD write_name.

    WRITE: / 'Own attribute', me->w_name.

    WRITE: / 'other object', io_obj->w_name.

  ENDMETHOD.                    "write_name

ENDCLASS.                    "lcl_car IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS lcl_bmw DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_bmw DEFINITION INHERITING FROM lcl_car.

ENDCLASS.                    "lcl_bmw DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_merc DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_merc DEFINITION INHERITING FROM lcl_car.

ENDCLASS.                    "lcl_merc DEFINITION


START-OF-SELECTION.
  DATA: lo_bmw   TYPE REF TO lcl_bmw,
        lo_merc  TYPE REF TO lcl_merc.

  CREATE OBJECT: lo_bmw,
                 lo_merc.

  lo_bmw->w_name  = 'BMW'.
  lo_merc->w_name = 'Merc'.

  lo_bmw->write_name( lo_merc ).

Regards,

Naimesh Patel

Read only

MarcinPciak
Active Contributor
0 Likes
536

It can also be used in generic calls. Suppose we have vehicle class.


CLASS lcl_vehicle DEFINITION.
   PUBLIC SECTION.
      METHODS: add_vehicle IMPORTING l_veh TYPE REF TO lcl_vehicle,
                        estimate_fuel.
      
      "table storing vehicles
      DATA: BEGIN OF it,
                   vehicle TYPE REF TO lcl_vehicle,
                 END OF it,
      
                 wa LIKE LINE OF it.
ENDCLASS.

CLASS lcl_vehicle IMPLEMENTATION.
    METHOD add_vehicle.
        wa-vehicle = l_veh.
        APPEND wa TO it.
    ENDMETHOD.

    METHOD estimate_fuel.
        LOOP at IT into WA.
           wa-vehicle->estimate_fuel( ).              "here we can call same method of different subclasses with different implementation
        ENDLOOP.
    ENDMETHOD.
ENDCLASS.

Now we create to subclasses.


CLASS lcl_plane DEFINITION INHERITING FROM lcl_vehicle.
   PUBLIC SECTION. 
      METHODS estimate_fuel REDEFINITION.
ENDCLASS.

CLASS lcl_ship DEFINITION INHERITING FROM lcl_vehicle.
   PUBLIC SECTION. 
      METHODS estimate_fuel REDEFINITION.
ENDCLASS.

Two different vehicles will redefine an estimate_fuel method as each has to take different factors into account when tanking up.

We would like to handle both plane and ship from one point. For this we add our new vehicles to the table of lcl_vehicle class.


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

CREATE OBJECT: r_vehicle, r_plane, r_ship.

"add all vehicles to the table
r_vehicle->add( r_plane ).
r_vehicle->add( r_ship ).

"Now we can perform generic call
r_vehicle->estimate_fuel( ).  "this will call same method of two subclasses with different implementation, from one point 

It would be easy now to handle new vehicle


CLASS lcl_motorbike DEFINITION INHERITING FROM lcl_vehicle.
   PUBLIC SECTION. 
      METHODS estimate_fuel REDEFINITION.
ENDCLASS.

DATA: r_motorbike TYPE REF TO lcl_motobike.

r_vehicle->add( r_motorbike ). "it is enough to add the vehicle, this already suits our model and appropriate method will be called for estimating fuel

Regards

Marcin

Edited by: Marcin Pciak on Mar 23, 2009 10:17 PM

Read only

Former Member
0 Likes
536

>

> Hi

>

> I've the following method def:

>

>

   METHODS : METH1 IMPORTING INPUT1 TYPE I   
>                                                               INPUT2 TYPE REF TO  CL_1.
> 

>

> where CL_1 is the class withing where the method is defined.

>

> Question is, what is this kind of definition and whats the use in real time implementation?

>

> thkx

> P.S

A simple example of this kind of definition which uses a Bank account class can be found in the below link (in Page 33)

[Eight Reasons Why Every ABAP Developer...|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/37c5db90-0201-0010-3a9b-d0a5288f3c15]