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

difference between abap object and function

Former Member
0 Likes
2,038

hi all,

i read the book on abap object of the difference between abap object and classical abap.

i know that there is only 1 instance of a specific function group but somehow i still not so clear why subsequent vehicle cannot use the same function. i also can use the do and loop to call the function? if cannot then why?

hope can get the advice.

thanks

                • using function *********

function-pool vehicle.

data speed type i value 0.

function accelerate.

speed = speed + 1.

endfunction.

function show_speed.

write speed.

endfunction.

report xx.

start-of-selection.

*vehicle 1

call function 'accelerate'.

call function 'accelerate'.

call function 'show_speed'.

*vehicle 2

??

*vehicle 3

??

*****abap object*******

report xx.

data: ov type ref to vehicle,

ov_tab type table of ref to vehicle.

start-of-selection.

do 5 times.

create object ov.

append ov to ov_tab.

enddo.

loop at ov_tab into ov.

do sy-tabix times.

call method ov->accelerate.

enddo.

call method ov->show_speed.

endloop.

14 REPLIES 14
Read only

Former Member
0 Likes
1,405

Hi,

From the function Module, no doubt, you can creat vehicles and store them in intrenal table. But suppose, u need to create different instances of same vehicle type and you need to trate them differently,

e.g. you need to calculate TAX for Trucks of same model but with different reg.numbers (form different states), you need to maintain instance number and all data related to that instance, which is very very tedious job.

While, with object orientation, you can create different instances with same object type, only you need to give different values to a attribute(say Reg number) and just call one method to calculate tax based on Reg Number.

With function Module, you need to create different methods for different behaviours.

Maintenance is tough. Difficult to add further functionaity after some bulk.

Advantages of OOPS:

1. Better Programming Structure

2. Real world entity can be modeled very well

3.Stress on data security and access

4. Data encapsulation and abstraction

5. Reduction in code redundancy

Also this may help you

http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5954f411d194a60000e8353423/content.htm

Hope, I gave clear idea.

Thanks,

Chetan Shah

Read only

0 Likes
1,405

hi chetan and other gurus,

first of all thanks for your explanation and your link. i already read that.

theoritically i can understand but i still somehow not 100% clear.

can show me with example when u say give different value to attribute for different instances. you can enhance the code that i provided or you can show me your own example. simple will be better.

thanks

Read only

Former Member
0 Likes
1,405

Hi,

How you will incorporate different accelerations of vehicles 1, 2 and 3, for single call of acceleration method, say vehicle 1 has one time acceleration, vehicle2 has 2 times acceleration, vehicle3 has 3 times ...

You have to create different functions like acc1 for vehicle1, acc2 for vehicle2 so on... and before calling you need to check for which vehicle you need to call the acceleration function.

While in Objects: you just create method name acceleration and code differently in different vehicle. While calling you need to just call one method "acceleration". No matter, afterwards you create 1000 different vehicles...just u need to define vehicles and its acceleration. While in functiona module, you need to create different 1000 functions and you need to check for those 1000 vehicle, call 1000 functions.....very very tedious job boss.....

This is just one example of power of OOPS....

I will also send you a code ...

Thanks,

Chetan Shah

Read only

0 Likes
1,405

hi chetan,

really appreciate your reply and i am waiting for your code so that i can get a clearer picture of what you have explained.

you are of great help and patient enough with my question.

thanks again.

Read only

0 Likes
1,405

Hi

Here the problem is you can create only one instance by fm of the same function group, so u can create only one vehicle, because if need to create a new vehicle you should reset the data of the old ones:

function-pool vehicle.

data speed type i value 0.

function accelerate.

speed = speed + 1.

endfunction.

function show_speed.

write speed.

endfunction.

function reset_speed.

clear speed.

endfunction.

report xx.

start-of-selection.

*vehicle 1

call function 'accelerate'.

call function 'accelerate'.

call function 'show_speed'.

*vehicle 2

call function 'reset_speed'.

call function 'accelerate'.

call function 'accelerate'.

call function 'show_speed'.

*vehicle 3

call function 'reset_speed'.

call function 'accelerate'.

call function 'accelerate'.

call function 'show_speed'.

But here it doesn't really create a new vehicle, so a new instance, but only clear a varible.

U should create 5 different function group to create really 5 separate instances and so 5 vehicles.

Max

Read only

0 Likes
1,405

hi,

i do the testing in system but the result are the same. i understand that function group/module only has 1 instance whereas object can have multiple instance of a class but i still do not clear of the difference. maybe this is not a good example. can anyone show me the difference by enhancing the following program?

thanks

        • abap object ****

REPORT ztest_vehicleoo .

CLASS vehicle DEFINITION.

PUBLIC SECTION.

METHODS accelerate.

METHODS show_speed.

PRIVATE SECTION.

DATA speed TYPE i VALUE 0.

ENDCLASS.

CLASS vehicle IMPLEMENTATION.

METHOD accelerate.

speed = speed + 1.

ENDMETHOD.

METHOD show_speed.

WRITE speed.

ENDMETHOD.

ENDCLASS.

DATA: ov TYPE REF TO vehicle,

ov_tab TYPE TABLE OF REF TO vehicle.

START-OF-SELECTION.

DO 5 TIMES.

CREATE OBJECT ov.

APPEND ov TO ov_tab.

ENDDO.

LOOP AT ov_tab INTO ov.

DO sy-tabix TIMES.

CALL METHOD ov->accelerate.

ENDDO.

CALL METHOD ov->show_speed.

ENDLOOP.

        • abap ****

i also created the function group and function module for this.

REPORT ZTEST_VEHICLE .

do 5 times.

call function 'Z_VEHICLE'.

call function 'Z_SHOWSPEED'.

enddo.

Read only

0 Likes
1,405

hi all,

can someone enhance below so that i can get the clear picture of the difference. currently with these 2, i do not see any difference in the output. tq

i know can have multiple instances of the same class whereas function only has 1 instance but below 2 i do not see the difference. i do hope gurus out there can enhance below 2 to make me see the difference. tq

        • abap object ****

REPORT ztest_vehicleoo .

CLASS vehicle DEFINITION.

PUBLIC SECTION.

METHODS accelerate.

METHODS show_speed.

PRIVATE SECTION.

DATA speed TYPE i VALUE 0.

ENDCLASS.

CLASS vehicle IMPLEMENTATION.

METHOD accelerate.

speed = speed + 1.

ENDMETHOD.

METHOD show_speed.

WRITE speed.

ENDMETHOD.

ENDCLASS.

DATA: ov TYPE REF TO vehicle,

ov_tab TYPE TABLE OF REF TO vehicle.

START-OF-SELECTION.

DO 5 TIMES.

CREATE OBJECT ov.

APPEND ov TO ov_tab.

ENDDO.

LOOP AT ov_tab INTO ov.

DO sy-tabix TIMES.

CALL METHOD ov->accelerate.

ENDDO.

CALL METHOD ov->show_speed.

ENDLOOP.

        • abap ****

i also created the function group and function module for this.

REPORT ZTEST_VEHICLE .

do 5 times.

call function 'Z_VEHICLE'.

call function 'Z_SHOWSPEED'.

enddo.

Read only

0 Likes
1,405

Hi

Don't warry!

If you've understood it, you've understood all.

Infact the next step it'll be:

Suppose you have to create a report where you need to use the same kind of object several times, for example several vehicles:

U need to check which vehicle has the higher speed.

It's easier to use the OO than function module, because you need only create a new instance for every veichle:

do 5 times.

create object ov.

append ov to ov_tab.

enddo.

loop at ov_tab into ov.

do sy-tabix times.

call method ov->accelerate.

enddo.

endloop.

*---> Check the higher speed:

DATA: BEGIN OF SPEED,

SPEED,

TABIX TYPE SY-TABIX,

END OF SPEED.

DO.

READ TABLE ov_tab into ov INDEX SY-INDEX.

IF SY-SUBRC <> 0. EXIT. ENDIF.

IF OV_TAB->SPEED > SPEED-SPEED.

SPEED-SPEED = OV_TAB->SPEED.

SPEED-TABIX = ST-TABIX.

ENDIF.

ENDDO.

READ TABLE OV_TAB INTO OV INDEX SPEED-TABIX.

Now you have the car with higher speed, but you have the data of all veichle too.

It's very hard by function group, because or you create as many function group as many veichles (but it means to repeat the same code) or you need to use some variable to store the speed of every veichle because the function group can store only one.

So u can do it the same thing using ABAP or OO ABAP, so you should decide which is more convenience

Max

Read only

0 Likes
1,405

hi,

i can understand what you have explained but when come to coding i really still not clear. can you show me by enhancing the program i provided?

thanks

Read only

0 Likes
1,405

Hi

Try this:

REPORT ZTEST_VEHICLEOO .

PARAMETERS: P_CAR TYPE I.

*---------------------------------------------------------------------*
*       CLASS vehicle DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS VEHICLE DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA: MAX_SPEED   TYPE I,
                MAX_VEHICLE TYPE I,
                NR_VEHICLES TYPE I.

    CLASS-METHODS CLASS_CONSTRUCTOR.
    METHODS CONSTRUCTOR.
    METHODS ACCELERATE.
    METHODS SHOW_SPEED.
  PRIVATE SECTION.
    DATA: SPEED      TYPE I VALUE 0,
          NR_VEHICLE TYPE I..
ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS vehicle IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS VEHICLE IMPLEMENTATION.
  METHOD CLASS_CONSTRUCTOR.
    NR_VEHICLES = 0.
  ENDMETHOD.

  METHOD CONSTRUCTOR.
    NR_VEHICLES = NR_VEHICLES + 1.
    NR_VEHICLE  = NR_VEHICLES.
  ENDMETHOD.

  METHOD ACCELERATE.
    SPEED = SPEED + 1.
    IF MAX_SPEED < SPEED.
      MAX_SPEED   = SPEED.
      MAX_VEHICLE = NR_VEHICLE.
    ENDIF.
  ENDMETHOD.
  METHOD SHOW_SPEED.
    WRITE: / 'Speed of vehicle nr.', NR_VEHICLE, ':', SPEED.
  ENDMETHOD.
ENDCLASS.


DATA: OV     TYPE REF TO VEHICLE,
      OV_TAB TYPE TABLE OF REF TO VEHICLE.

DATA: V_TIMES TYPE I,
      FL_ACTION.

START-OF-SELECTION.
  DO P_CAR TIMES.
    CREATE OBJECT OV.
    APPEND OV TO OV_TAB.
  ENDDO.

  LOOP AT OV_TAB INTO OV.

    IF FL_ACTION = SPACE.
      FL_ACTION = 'X'.
      V_TIMES = SY-TABIX * 2.
    ELSE.
      FL_ACTION = SPACE.
      V_TIMES = SY-TABIX - 2.
    ENDIF.

    DO V_TIMES TIMES.
      CALL METHOD OV->ACCELERATE.
    ENDDO.
    CALL METHOD OV->SHOW_SPEED.
  ENDLOOP.

  SKIP.

  WRITE: / 'Higher speed', VEHICLE=>MAX_SPEED, 'for vehicle nr.',
            VEHICLE=>MAX_VEHICLE.

Max

Read only

0 Likes
1,405

hi max,

thanks. i will try it tomorrow. in the meantime, would you be able to show why we cannot use function as we cannot have multiple instances of function and need to use the same function again and again which is tedious as what you said.

i will reward all who have helped me.

thanks again

Read only

0 Likes
1,405

Hi

Now try this:

REPORT ZTEST_VEHICLEOO .

PARAMETERS: P_CAR   TYPE I,
            P_READ  TYPE I.

*---------------------------------------------------------------------*
*       CLASS vehicle DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS VEHICLE DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA: MAX_SPEED   TYPE I,
                MAX_VEHICLE TYPE I,
                NR_VEHICLES TYPE I.

    CLASS-METHODS CLASS_CONSTRUCTOR.

    METHODS CONSTRUCTOR.
    METHODS ACCELERATE.
    METHODS SHOW_SPEED.
    METHODS GET_SPEED EXPORTING E_SPEED TYPE I.
  PRIVATE SECTION.
    DATA: SPEED      TYPE I,
          NR_VEHICLE TYPE I..
ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS vehicle IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS VEHICLE IMPLEMENTATION.
  METHOD CLASS_CONSTRUCTOR.
    NR_VEHICLES = 0.
  ENDMETHOD.

  METHOD CONSTRUCTOR.
    NR_VEHICLES = NR_VEHICLES + 1.
    NR_VEHICLE  = NR_VEHICLES.
  ENDMETHOD.

  METHOD ACCELERATE.
    SPEED = SPEED + 1.
    IF MAX_SPEED < SPEED.
      MAX_SPEED   = SPEED.
      MAX_VEHICLE = NR_VEHICLE.
    ENDIF.
  ENDMETHOD.
  METHOD SHOW_SPEED.
    WRITE: / 'Speed of vehicle nr.', NR_VEHICLE, ':', SPEED.
  ENDMETHOD.

  METHOD GET_SPEED.
    E_SPEED = SPEED.
  ENDMETHOD.

ENDCLASS.


DATA: OV     TYPE REF TO VEHICLE,
      OV_TAB TYPE TABLE OF REF TO VEHICLE.

DATA: V_TIMES TYPE I,
      FL_ACTION.

DATA: V_SPEED TYPE I.

START-OF-SELECTION.
  DO P_CAR TIMES.
    CREATE OBJECT OV.
    APPEND OV TO OV_TAB.
  ENDDO.

  LOOP AT OV_TAB INTO OV.

    IF FL_ACTION = SPACE.
      FL_ACTION = 'X'.
      V_TIMES = SY-TABIX * 2.
    ELSE.
      FL_ACTION = SPACE.
      V_TIMES = SY-TABIX - 2.
    ENDIF.

    DO V_TIMES TIMES.
      CALL METHOD OV->ACCELERATE.
    ENDDO.
    CALL METHOD OV->SHOW_SPEED.
  ENDLOOP.

  SKIP.

  WRITE: / 'Higher speed', VEHICLE=>MAX_SPEED, 'for vehicle nr.',
            VEHICLE=>MAX_VEHICLE.

  SKIP.

  READ TABLE OV_TAB INTO OV INDEX P_READ.
  IF SY-SUBRC <> 0.
    WRITE: 'No vehicle', P_READ.
  ELSE.
    CALL METHOD OV->GET_SPEED IMPORTING E_SPEED = V_SPEED.
    WRITE: 'Speed of vehicle', P_READ, V_SPEED.
  ENDIF.

Try to repeat this using a function group and I think you'll undestand because it'll be very hard to do it.

By only one function group how can u read the data of a certain vehicle?

Yes you can create in the function group an internal table where u store the data of every car: in this way u use the internal table like it was an instance, but you should consider here the example is very simple. Here we have only the speed as characteristic, but really we can have many complex characteristics.

Max

Read only

0 Likes
1,405

hi max,

i will test it tomorrow.

thanks very much of your effort.

rgds

Read only

Former Member
0 Likes
1,405

Hi,

Check this link from the standard sap help for the difference between abap object and function.

<b>Function module</b>

http://help.sap.com/saphelp_nw2004s/helpdata/en/73/d18756b27011d194ef0000e8353423/content.htm

<b>Class introductory example</b>

http://help.sap.com/saphelp_nw2004s/helpdata/en/73/d18759b27011d194ef0000e8353423/content.htm

<b>Object introductory example</b>

http://help.sap.com/saphelp_nw2004s/helpdata/en/48/ad3779b33a11d194f00000e8353423/content.htm

In the object introductory example link ...Press forward (>) in the page to view the step by step procedure..

Thanks,

Naren