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

Abap Object

Former Member
0 Likes
864

Hi All,

Can any one tell about the use of Interface in Abap Objects With Examples. Thanks.

1 ACCEPTED SOLUTION
Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
797

Hi,

Interfaces is a very usful concept. SAP's BADI concept is also based on this.

In an interface we just provide a specification. That is an interface will only have method definitions and some data. Basically you create an interface when you want to create a behaviour. Something like say a vehicle which is Movable so you create an interface Movalbe with neccesary methods. But the implementation for the methods in this interface will be provided by different vehicles in different ways. Thats is CAR, PLANE will have their own implementations for the method of MOVABLE.

So by implementing the interface a class says I support this behaviour.

Example:

Interface <b>movable</b>.

methods: move importing name.

endinterface.

class <b>car</b> definition.

public section.

interfaces: movable.

endclass.

class car implementation.

method <b>move</b> importing name type string.

*code here

endmethod.

Regards,

Sesh

endclass.

7 REPLIES 7
Read only

uwe_schieferstein
Active Contributor
0 Likes
797

Hello Vijay

The following sample report shows you the strength of interfaces. Depending on the document key the factory method <b>cl_wb2_doc_factory=>get_doc</b> returns completely different documents yet you need only a <b>single interface variable</b> to hold all of them.

[code]&----


*& Report ZUS_SDN_ABAP_OO_INTERFACE

*&

&----


*&

*&

&----


REPORT zus_sdn_abap_oo_interface.

TYPE-POOLS: abap.

DATA:

gd_text TYPE wb2_flow_text,

gd_doc_key TYPE wb2_doc_key, " char20

gif_doc TYPE REF TO if_wb2_doc. " document

PARAMETERS:

p_order TYPE vbeln DEFAULT '0000006127'.

  • NOTE: Standard sales order 0000006127 -> has

  • Delivery 0080006160 -> has

  • Invoice 0090017459 -> has

  • Accouting document 0100012183

START-OF-SELECTION.

  • Get sales order

CLEAR: gd_doc_key.

gd_doc_key+0(2) = 'A'. " sales order

gd_doc_key+2(10) = p_order.

gif_doc = cl_wb2_doc_factory=>get_doc( gd_doc_key ).

PERFORM display_doc_text.

  • Get delivery

CLEAR: gd_doc_key.

gd_doc_key+0(2) = 'C'. " outbound delivery

gd_doc_key+2(10) = '0080006160'.

gif_doc = cl_wb2_doc_factory=>get_doc( gd_doc_key ).

PERFORM display_doc_text.

  • Get invoice

CLEAR: gd_doc_key.

gd_doc_key+0(2) = 'K'. " sales invoice

gd_doc_key+2(10) = '0090017459'.

gif_doc = cl_wb2_doc_factory=>get_doc( gd_doc_key ).

PERFORM display_doc_text.

  • Get accounting document

CLEAR: gd_doc_key.

gd_doc_key+0(2) = 'H'. " accounting document

gd_doc_key+2(4) = '1000'.

gd_doc_key+6(10) = '0100012183'.

gd_doc_key+16(4) = '1999'.

gif_doc = cl_wb2_doc_factory=>get_doc( gd_doc_key ).

PERFORM display_doc_text.

END-OF-SELECTION.

&----


*& Form DISPLAY_DOC_TEXT

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM display_doc_text .

gd_text = gif_doc->get_text( abap_false ).

MESSAGE gd_text TYPE 'I'.

ENDFORM. " DISPLAY_DOC_TEXT

*----


    • CLASS doc_type DEFINITION

*----


    • types of objects *

*----


*CLASS lcls_doc_type DEFINITION.

  • PUBLIC SECTION.

  • CONSTANTS:

    • all document types

  • all TYPE wb2_doc_type VALUE '*',

    • trading contract

  • tc TYPE wb2_doc_type VALUE 'Q',

    • trading contract in memory

  • tc_mem TYPE wb2_doc_type VALUE 'Q1',

    • sales order

  • so TYPE wb2_doc_type VALUE 'A',

    • sales contract

  • sc TYPE wb2_doc_type VALUE 'B',

    • purchase order

  • po TYPE wb2_doc_type VALUE 'C',

    • outbound delivery

  • od TYPE wb2_doc_type VALUE 'D',

    • inbound delivery

  • id TYPE wb2_doc_type VALUE 'E',

    • vendor billing document

  • vb TYPE wb2_doc_type VALUE 'F',

    • goods receive

  • gr TYPE wb2_doc_type VALUE 'G',

    • invoice verification

  • iv TYPE wb2_doc_type VALUE 'H',

    • accounting document

  • ac TYPE wb2_doc_type VALUE 'I',

    • agency document

  • settlement_request TYPE wb2_doc_type VALUE 'J',

    • agency document

  • settlement_request_list TYPE wb2_doc_type VALUE 'J1',

    • agency document

  • posting_list TYPE wb2_doc_type VALUE 'J2',

    • agency document

  • remuneration_list TYPE wb2_doc_type VALUE 'J3',

    • sales invoice

  • si TYPE wb2_doc_type VALUE 'K',

    • goods issue

  • gi TYPE wb2_doc_type VALUE 'L',

    • picking request

  • pr TYPE wb2_doc_type VALUE 'M',

    • purchase contract

  • po_contract TYPE wb2_doc_type VALUE 'N',

    • purchase: request for quotation

  • po_request TYPE wb2_doc_type VALUE 'O',

    • purchase: scheduling agreement

  • po_scheduling TYPE wb2_doc_type VALUE 'P',

    • creditor memo ( special form of SI )

  • credit_memo TYPE wb2_doc_type VALUE 'K1',

    • debitor memo ( special form of SI )

  • debit_memo TYPE wb2_doc_type VALUE 'K2',

    • request for creditor memo ( special form of SO )

  • credit_memo_request TYPE wb2_doc_type VALUE 'A1',

    • request for debitor memo ( special form of SO )

  • debit_memo_request TYPE wb2_doc_type VALUE 'A2',

    • inquiry ( SO data )

  • inquiry TYPE wb2_doc_type VALUE 'A3',

    • quotation ( SO data )

  • quotation TYPE wb2_doc_type VALUE 'A4',

    • scheduling agreement ( SO data )

  • so_scheduling TYPE wb2_doc_type VALUE 'A5',

    • returns ( SO data )

  • returns TYPE wb2_doc_type VALUE 'A6',

    • invoice cancellation ( SI data )

  • si_cancel TYPE wb2_doc_type VALUE 'K3',

    • Credit memo cancellation ( SI data )

  • credit_memo_cancel TYPE wb2_doc_type VALUE 'K4',

    • Returns delivery for order ( delivery )

  • returns_delivery TYPE wb2_doc_type VALUE 'E1',

    • Pro forma invoice (SI data )

  • pro_forma_invoice TYPE wb2_doc_type VALUE 'K5',

    • Independent reqts plan ( SO data )

  • reqts_plan TYPE wb2_doc_type VALUE 'A7',

    • invoice list ( vbrk + vbrl )

  • invoice_list TYPE wb2_doc_type VALUE 'K6',

    • credit memo list ( vbrk + vbrl )

  • credit_memo_list TYPE wb2_doc_type VALUE 'K7',

    • Intercompany invoice

  • intercompany_invoice TYPE wb2_doc_type VALUE 'K8',

    • Intercompany credit memo

  • intercompany_credit_memo TYPE wb2_doc_type VALUE 'K9',

    • material movement ( only for internal use )

  • material_movement_tmp TYPE wb2_doc_type VALUE 'L$',

    • material movement ( not GI or GR )

  • material_movement TYPE wb2_doc_type VALUE 'L1',

    • temporary GR from PO

  • gr_from_po_tmp TYPE wb2_doc_type VALUE 'S1',

    • temporary GR from ID

  • gr_from_id_tmp TYPE wb2_doc_type VALUE 'S2',

    • unused document type, never written onto DB

  • unused TYPE wb2_doc_type VALUE '##',

    • currency forward

  • cf TYPE wb2_doc_type VALUE 'T',

    • trading contract sales side

  • tc_so TYPE wb2_doc_type VALUE 'Q1',

    • not specified

  • not_specified TYPE wb2_doc_type VALUE 'XX'.

*

*ENDCLASS. "lcls_doc_type DEFINITION[/code]

Regards

Uwe

Read only

seshatalpasai_madala
Product and Topic Expert
Product and Topic Expert
0 Likes
798

Hi,

Interfaces is a very usful concept. SAP's BADI concept is also based on this.

In an interface we just provide a specification. That is an interface will only have method definitions and some data. Basically you create an interface when you want to create a behaviour. Something like say a vehicle which is Movable so you create an interface Movalbe with neccesary methods. But the implementation for the methods in this interface will be provided by different vehicles in different ways. Thats is CAR, PLANE will have their own implementations for the method of MOVABLE.

So by implementing the interface a class says I support this behaviour.

Example:

Interface <b>movable</b>.

methods: move importing name.

endinterface.

class <b>car</b> definition.

public section.

interfaces: movable.

endclass.

class car implementation.

method <b>move</b> importing name type string.

*code here

endmethod.

Regards,

Sesh

endclass.

Read only

Former Member
0 Likes
797

Hi Vijay,

Interface , as the name suggests, is the external point of contact for the class and it will not have an implementation part.The implementation will be done in the class, in which this interface is defined.

Usually , the public components (attributes, methods etc)of the class constitute an interface.

Taking a real-time scenario to explain interface.

Consider a car. The accessories of the car form the interface. eg: you are using the steering wheel to change the direction of the car, without knowing the internal working . So each of the accessories form the component of the interface.

You can go through these docs to learn more about interfaces.

http://esnips.com/doc/39fdc647-1aed-4b40-a476-4d3042b6ec28/class_builder.ppt

http://esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt

Regards,

SP.

Read only

Former Member
0 Likes
797

Hi Vijay,

Interfaces are used to define uniform protocols for services. Various classes can implement these services in different ways, but with the semantics unchanged.

Use of interfaces are as follows :

1. Generalisation/Specialisation relationship can be realised using Interfaces.

2. Polymorphism can be performed with interfaces.

3. Multiple inheritence can be simulated using interfaces in ABAP Objects.

Example program.

&----


*& Interface (Definiton) zl_lif_vehicle

&----


  • Definition of Vehicle Interface.

  • This interface will be used for car and bike class.

  • Interfaces contain no implementation.

----


interface zl_lif_vehicle.

  • No visibility levels in interface. All components are by default public

methods: set_make importing value(im_make) type string

im_model type string,

get_make exporting value(ex_make) type string

ex_model type string.

endinterface. "zl_lif_vehicle

&----


*& Class (Definiton) zl_lcl_car

&----


  • Definition of Car class

----


class zl_lcl_car definition.

public section.

  • To specify the interface name in the public section of the class as

  • interfaces can only be implemented publicly

interfaces zl_lif_vehicle.

private section.

data: gv_make type string, " Vehicle make

gv_model type string. " Type or model

endclass. "zl_lcl_car DEFINITION

&----


*& Class (Definiton) zl_lcl_bike

&----


  • Definition of Bike class

----


class zl_lcl_bike definition.

public section.

  • To specify the interface name in the public section of the class as

  • interfaces can only be implemented publicly

interfaces zl_lif_vehicle.

  • To simplify accessing interface components, aliases are used

aliases: bike_set_make for zl_lif_vehicle~set_make,

bike_get_make for zl_lif_vehicle~get_make.

private section.

data: gv_make type string, " Vehicle make

gv_model type string. " Type or model

endclass. "zl_lcl_bike DEFINITION

&----


*& Class (Implementation) zl_lcl_car

&----


  • Implementation of Car class

----


class zl_lcl_car implementation.

  • Implementing the interface components in the class. The interface

  • components are distinguished from other components in implementing class

  • by prefixing interface name followed by a tilde ~

method zl_lif_vehicle~set_make.

if im_make is not initial

and im_model is not initial.

gv_make = im_make.

gv_model = im_model.

endif.

endmethod. "set_make

method zl_lif_vehicle~get_make.

ex_make = gv_make.

ex_model = gv_model.

endmethod. "get_make

endclass. "zl_lcl_car IMPLEMENTATION

&----


*& Class (Implementation) zl_lcl_bike

&----


  • Implementation of Bike class

----


class zl_lcl_bike implementation.

method zl_lif_vehicle~set_make.

if im_make is not initial

and im_model is not initial.

gv_make = im_make.

gv_model = im_model.

endif.

endmethod. "set_make

method zl_lif_vehicle~get_make.

ex_make = gv_make.

ex_model = gv_model.

endmethod. "get_make

endclass. "zl_lcl_bike IMPLEMENTATION

  • Declaring reference variables with reference to the car and bike class.

  • These vareiable contain a reference to an object of the classes

data: z_car type ref to zl_lcl_car,

z_bike type ref to zl_lcl_bike.

data : gv_make type string,

gv_model type string.

start-of-selection.

  • Create instance of the car and bike class

create object: z_car,

z_bike.

  • Accessing the interface components by using an object reference

  • whose class implements the interface.

&----


  • For car class

  • Method 1 - Access using the interface name followed by ~

&----


z_car->zl_lif_vehicle~set_make( exporting im_make = 'HYUNDAI'

im_model = 'SANTRO' ).

z_car->zl_lif_vehicle~get_make( importing ex_make = gv_make

ex_model = gv_model ).

write: / 'Make :', gv_make,

/ 'Model :', gv_model.

&----


  • For bike class

  • Method 2 - Access using ~

  • Since aliases have been defined in bike class for interface

  • components, the same can be accessed using alais names

&----


z_bike->bike_set_make( exporting im_make = 'SUZUKI'

im_model = 'HAYABUSA' ).

z_bike->bike_get_make( importing ex_make = gv_make

ex_model = gv_model ).

write: / 'Make :', gv_make,

/ 'Model :', gv_model.

Award points if found useful.

Regards

Indrajit

Read only

Former Member
0 Likes
797

check the below links lot of info and examples r there

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

http://www.geocities.com/victorav15/sapr3/abap_ood.html

http://www.brabandt.de/html/abap_oo.html

Check this cool weblog:

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

/people/thomas.jung3/blog/2004/12/08/abap-persistent-classes-coding-without-sql

/people/sap.user72/blog/2005/05/10/a-small-tip-for-the-beginners-in-oo-abap

/people/ravikumar.allampallam/blog/2005/02/11/abap-oo-in-action

/people/thomas.jung3/blog/2005/09/08/oo-abap-dynpro-programming

http://help.sap.com/saphelp_nw04/helpdata/en/c3/225b6254f411d194a60000e8353423/frameset.htm

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

http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://www.esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt

http://www.allsaplinks.com/

http://www.sap-img.com/

http://www.sapgenie.com/

http://help.sap.com

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

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

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

http://www.esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

http://www.esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf

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

http://help.sap.com/saphelp_erp2005/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

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

these links

http://help.sap.com/saphelp_47x200/helpdata/en/ce/b518b6513611d194a50000e8353423/content.htm

For funtion module to class

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

for classes

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm

for methods

http://help.sap.com/saphelp_47x200/helpdata/en/08/d27c03b81011d194f60000e8353423/content.htm

for inheritance

http://help.sap.com/saphelp_47x200/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htm

for interfaces

http://help.sap.com/saphelp_47x200/helpdata/en/c3/225b6254f411d194a60000e8353423/content.htm

For Materials:

1) http://help.sap.com/printdocu/core/Print46c/en/data/pdf/BCABA/BCABA.pdf -- Page no: 1291

2) http://esnips.com/doc/5c65b0dd-eddf-4512-8e32-ecd26735f0f2/prefinalppt.ppt

3) http://esnips.com/doc/2c76dc57-e74a-4539-a20e-29383317e804/OO-abap.pdf

4) http://esnips.com/doc/0ef39d4b-586a-4637-abbb-e4f69d2d9307/SAP-CONTROLS-WORKSHOP.pdf

5) http://esnips.com/doc/92be4457-1b6e-4061-92e5-8e4b3a6e3239/Object-Oriented-ABAP.ppt

6) http://esnips.com/doc/448e8302-68b1-4046-9fef-8fa8808caee0/abap-objects-by-helen.pdf

7) http://esnips.com/doc/39fdc647-1aed-4b40-a476-4d3042b6ec28/class_builder.ppt

😎 http://www.amazon.com/gp/explorer/0201750805/2/ref=pd_lpo_ase/102-9378020-8749710?ie=UTF8

1) http://www.erpgenie.com/sap/abap/OO/index.htm

2) http://help.sap.com/saphelp_nw04/helpdata/en/ce/b518b6513611d194a50000e8353423/frameset.htm

Lots of material can be found on www.esnips.com

Thanks&Regards,

Balaji Reddy G

***Rewards for helpful answers