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: 

I need a simple addition program on OOPS ABAP

0 Kudos

Dear sir,

I need a simple addition of two numbers program on OOPS ABAP concept. including the class and method creation methods, and the object creation methods.. if possible give me the sample programs on OOPS ABAP.

regards

patil.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Rachu,

Hope the below program will help you..

CLASS C_COUNTER DEFINITION.

PUBLIC SECTION.

METHODS: SET_COUNTER IMPORTING VALUE(SET_VALUE) TYPE I,

INCREMENT_COUNTER,

GET_COUNTER EXPORTING VALUE(GET_VALUE) TYPE I.

PRIVATE SECTION.

DATA COUNT TYPE I.

ENDCLASS.

CLASS C_COUNTER IMPLEMENTATION.

METHOD SET_COUNTER.

COUNT = SET_VALUE.

ENDMETHOD.

METHOD INCREMENT_COUNTER.

ADD 1 TO COUNT.

ENDMETHOD.

METHOD GET_COUNTER.

GET_VALUE = COUNT.

ENDMETHOD.

ENDCLASS

Regards,

Nikita

11 REPLIES 11

Former Member
0 Kudos

hi,

CLASS PARENT1 DEFINITION.

PUBLIC SECTION.

DATA : A TYPE I VALUE 2, B TYPE I VALUE 1, C TYPE I.

METHODS : ADD.

ENDCLASS.

CLASS PARENT1 IMPLEMENTATION.

METHOD ADD.

C = A + B.

WRITE C.

ENDMETHOD.

ENDCLASS.

CLASS PARENT2 DEFINITION.

PUBLIC SECTION.

DATA : A TYPE I VALUE 2, B TYPE I VALUE 1, C TYPE I.

METHODS : ADD.

ENDCLASS.

CLASS PARENT2 IMPLEMENTATION.

METHOD ADD.

C = A - B.

WRITE C.

ENDMETHOD.

ENDCLASS.

CLASS CHILD DEFINITION INHERITING FROM PARENT1.

PUBLIC SECTION.

DATA D TYPE I.

ENDCLASS.

CLASS CHILD IMPLEMENTATION.

ENDCLASS.

DATA : R1 TYPE REF TO CHILD.

START-OF-SELECTION.

CREATE OBJECT R1.

CALL METHOD R1->ADD.

Regards,

Vikram.S

0 Kudos

Dear Vikram sir,

Thanks for giving a sample code for me sir.. sir this code is executed in SE38 or some other TCODE to write the code..,(if it in SE38 only sir we have to select either executable type or some other sir..) or we need in some other TCODE for to execute this program.. plz give me the hints sir..

0 Kudos

Hi,

SE24 is the TCODE for Class/Interface browser and editor.

Look at the documentation for CLASS keyword in ABAPDOCU tcode.

Regards,

Sesh

Former Member
0 Kudos

hi Rachu,

please chk this site.

saptechnical.com/Tutorials/ABAP/OOP/Example1.htm

hope it may help you.

thanks n regards

Sachin

Former Member
0 Kudos

Hi Rachu,

Hope the below program will help you..

CLASS C_COUNTER DEFINITION.

PUBLIC SECTION.

METHODS: SET_COUNTER IMPORTING VALUE(SET_VALUE) TYPE I,

INCREMENT_COUNTER,

GET_COUNTER EXPORTING VALUE(GET_VALUE) TYPE I.

PRIVATE SECTION.

DATA COUNT TYPE I.

ENDCLASS.

CLASS C_COUNTER IMPLEMENTATION.

METHOD SET_COUNTER.

COUNT = SET_VALUE.

ENDMETHOD.

METHOD INCREMENT_COUNTER.

ADD 1 TO COUNT.

ENDMETHOD.

METHOD GET_COUNTER.

GET_VALUE = COUNT.

ENDMETHOD.

ENDCLASS

Regards,

Nikita

0 Kudos

Hi Nikita,

Thanks for giving example.. but i am getting confusing where to execute this code as in SE38 or in other editor, is there other TCODE to write this code & execute the program.

regards

rachu.

0 Kudos

Hi Rachu,

Yes you need to write this code in se38 & execute in se38..

Here is one more program for your reference....

CLASS counter DEFINITION.

PUBLIC SECTION.

METHODS: set IMPORTING value(set_value) TYPE i,

increment,

get EXPORTING value(get_value) TYPE i.

PRIVATE SECTION.

DATA count TYPE i.

ENDCLASS.

CLASS counter IMPLEMENTATION.

METHOD set.

count = set_value.

ENDMETHOD.

METHOD increment.

ADD 1 TO count.

ENDMETHOD.

METHOD get.

get_value = count.

ENDMETHOD.

ENDCLASS.

DATA number TYPE i VALUE 5.

DATA cnt TYPE REF TO counter.

START-OF-SELECTION.

CREATE OBJECT cnt.

CALL METHOD cnt->set EXPORTING set_value = number.

DO 3 TIMES.

CALL METHOD cnt->increment.

ENDDO.

CALL METHOD cnt->get IMPORTING get_value = number.

WRITE number.

Regards,

Nikita

Edited by: Nikita Jain on Oct 13, 2008 11:47 AM

0 Kudos

Hi Nikita,

Thanks for giving examples it really help for me.if you have some documents on OOPS ABAP please send it to patil.rachu at gmail.com.

regards

rachu.

0 Kudos

Hi Nikita,

How to Handle error conditions in OOPS ABAP (TRY CATCH), statements how to use them in program?

regards

rachu.

0 Kudos

Hi Rachu,

I have sent an ABAP e-book on your email id mentioned in your post. In that page 1293 onwards, you can find the OOPS ABAP documentation.

Regards,

Nikita

Former Member
0 Kudos

Hi,

Goto tcode se38. Create a program.after this check this sample code


REPORT z_sdn.


*----------------------------------------------------------------------*
*       CLASS lcl_test DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_test DEFINITION.
  PUBLIC SECTION.
    METHODS: constructor IMPORTING w_num1 TYPE i
                                   w_num2 TYPE i,
             calculate.
  PRIVATE SECTION.
    DATA: num1 TYPE i,
          num2 TYPE i,
          res  TYPE i.
ENDCLASS.                    "lcl_test DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_test IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_test IMPLEMENTATION.
  METHOD constructor.
    num1 = w_num1.
    num2 = w_num2.
  ENDMETHOD.

  METHOD calculate.
    res = num1 + num2.
    WRITE: res.
  ENDMETHOD.
ENDCLASS.                    "lcl_test IMPLEMENTATION


PARAMETERS:
  p_num1 TYPE i,
  p_num2 TYPE i.
DATA:
  r_test TYPE REF TO lcl_test.

START-OF-SELECTION.
CREATE OBJECT r_test EXPORTING
                        w_num1 = p_num1
                        w_num2 = p_num2.

r_test->calculate( ).

Regards

Abhijeet