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

about classes

Former Member
0 Likes
537

hi any one help me how to declare class in abap ? like in c..

plz send simple example of it.

4 REPLIES 4
Read only

SantoshKallem
Active Contributor
0 Likes
502

go thru with sample

Template for making a class

Delete the parts that should not be used

******************************************

  • Definition part

******************************************

CLASS xxx DEFINITION.

*----


  • Public section

*----


PUBLIC SECTION.

TYPES:

DATA:

  • Static data

CLASS-DATA:

  • Methods

METHODS:

  • Using the constructor to initialize parameters

constructor IMPORTING xxx type yyy,

  • Method with parameters

mm1 IMPORTING iii TYPE ttt.

  • Method without parameters

mm2.

  • Static methods

CLASS-METHODS:

----


  • Protected section. Also accessable by subclasses

*----


PROTECTED SECTION.

*----


  • Private section. Not accessable by subclasses

*----


PRIVATE SECTION.

ENDCLASS.

******************************************

  • Implementation part

******************************************

CLASS lcl_airplane IMPLEMENTATION.

METHOD constructor.

ENDMETHOD.

METHOD mm1.

ENDMETHOD.

METHOD mm2.

ENDMETHOD.

ENDCLASS.

Template for calling a class

  • Create reference to class lcl_airplane

DATA: airplane1 TYPE REF TO lcl_airplane.

START-OF-SELECTION.

  • Create instance using parameters in the cosntructor method

CREATE OBJECT airplane1 exporting im_name = 'Hansemand'

im_planetype = 'Boing 747'.

  • Calling a method with parameters

CALL METHOD: airplane1->display_n_o_airplanes,

airplane1->display_attributes.

Subclass

CLASS xxx DEFINITION INHERITING FROM yyy.

Using af class as a parameter for a method

The class LCL_AIRPLANE is used as a parameter for method add_a_new_airplane:

METHODS:

add_a_new_airplane importing im_airplane TYPE REF to lcl_airplane.

Interfaces

In ABAP interfaces are implemented in addition to, and independently of classes. An interface only has a declaration part,

and do not have visibility sections. Components (Attributes, methods, constants, types) can be defined the same way as in classes.

• Interfaces are listed in the definition part lof the class, and must always be in the PUBLIC SECTION.

• Operations defined in the interface atre impemented as methods of the class. All methods of the interface

must be present in the implementation part of the class.

• Attributes, events, constants and types defined in the interface are automatically available to the class

carrying out the implementation.

• Interface components are addressed in the class by <interface name>~<component name>

Example of how to implement an interface:

INTERFACE lif_document

DATA: author type ref to lcl_author.

METHODS: print,

display.

ENDINTERFACE.

CLASS lcl_text_document DEFINITION.

PUBLIC SECTION.

INTERFACES lif_document.

METHODS display.

ENDCLASS.

CLASS lcl_text_document IMPLEMENTTION.

METHOD lif_document~print.

ENDMETHOD.

METHOD lif_document~display

ENDMETHOD.

METHOD display.

ENDMETHOD.

ENDCLASS.

REPORT zzz.

DATA: text_doc TYPE REF TO lcl_document.

Start-of-selection.

CREATE OBJECT text_doc.

CALL METHOD text_doc->lif_document~print.

CALL METHOD text_doc->lif_document~display.

CALL METHOD text_doc->display.

Reward if useful.

cheers.

santhosh

Read only

Former Member
0 Likes
502

Hi Rohan ,

Here is a sample class definition

CLASS L_CL_MATNR DEFINITION .
*--> Declare attributes of class
  PUBLIC SECTION .
    DATA : G_T_MATNR TYPE TABLE OF G_TY_MATNR ,
           G_WA_MATNR TYPE G_TY_MATNR.
    EVENTS : NO_DATA_FOUND.
    METHODS : GET_MATERIAL_DETAILS ,
              DISPLAY_OUTPUT,
              DISPLAY_ERROR FOR EVENT NO_DATA_FOUND OF L_CL_MATNR.

  PRIVATE SECTION.

*    METHODS :  display_error FOR EVENT no_data_found OF l_cl_matnr.

ENDCLASS.

*---------------------------------------------------------------------*
*       CLASS l_cl_matnr IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
CLASS L_CL_MATNR IMPLEMENTATION.
*--> Implement Method get material
  METHOD GET_MATERIAL_DETAILS .
    SELECT MATNR WERKS LGORT LABST
     INTO TABLE G_T_MATNR
     FROM MARD
     WHERE MATNR IN SO_MATNR AND
           WERKS IN SO_WERKS.
    IF SY-SUBRC <> 0.
      RAISE EVENT NO_DATA_FOUND.
    ENDIF.

  ENDMETHOD.

*--> Implement Method display_output
  METHOD DISPLAY_OUTPUT .


  ENDMETHOD.


*--> Implement Method display_error
  METHOD DISPLAY_ERROR .
    WRITE : 'error'.
  ENDMETHOD.

ENDCLASS.

so it has 2 parts one for DEFINITION and second for implementation.

Dont forget to reward points if reply is helpful.

Regards

Arun

Read only

Former Member
0 Likes
502

try this link it will be very useful.....

regds,

kiran

Read only

Former Member
0 Likes
502

hi,

Here is the sample program.

REPORT zmmr_nm_prd_ord_label .

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

PARAMETERS : p_werks TYPE werks_d ,

p_aufnr TYPE aufnr ,

p_tank TYPE char1 .

SELECTION-SCREEN END OF BLOCK b1.

*Class declaration.

CLASS prd_ord_label DEFINITION.

PUBLIC SECTION.

DATA : v_maktx TYPE matnr,

v_pdesc TYPE name2,

v_matnr TYPE matnr.

METHODS : get_data IMPORTING e_werks TYPE werks_d

e_aufnr TYPE aufnr,

print_label IMPORTING e_ab1 TYPE n

e_ab2 TYPE char20

e_werks TYPE werks_d

e_devic TYPE sypdest

e_date TYPE sy-datum.

ENDCLASS.

CLASS prd_ord_label IMPLEMENTATION.

METHOD get_data.

CLEAR : v_matnr, v_maktx, v_pdesc, it_charg,x_charg.

SELECT SINGLE plnbez FROM afko INTO v_matnr WHERE aufnr = e_aufnr.

SELECT SINGLE maktx FROM makt INTO v_maktx WHERE matnr = v_matnr.

SELECT SINGLE name2 FROM t001w INTO v_pdesc WHERE werks = e_werks.

SELECT charg FROM afpo INTO TABLE it_charg WHERE aufnr = e_aufnr.

ENDMETHOD.

METHOD print_labels.

DO 51 TIMES.

IF v_c1 < e_lb1.

v_c1 = v_c1 + 1.

CALL METHOD print_label EXPORTING e_ab1 = e_lb1

e_ab2 = e_lb2

e_werks = e_werks

e_devic = e_devic

e_date = e_date.

ENDIF.

ENDDO.

ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

DATA : o_prdlbl TYPE REF TO prd_ord_label.

CREATE OBJECT o_prdlbl.

CALL METHOD o_prdlbl->get_data EXPORTING e_werks = p_werks

e_aufnr = p_aufnr.

..........

...........

Hope this helps.

Regards,

Richa