‎2007 May 23 8:40 AM
hi
i am new to ABAP objects.
could anyone pls give me a simple explaination on Interface and a simple example.
i have referred books and materials, but it didnt sound so clear for me.
so if anyone could give me an explaination in simple language (layman lang),
it would be really helpful for me
<b>points awarded</b>
thanks in advance
Sreeni
‎2007 May 23 9:37 AM
Hi Srinivasan,
Interfaces differ from regular inheritance in their areas of use. There are
hardly any differences in terms of programming.
From a technical perspective, interfaces are simply superclasses that
cannot be instantiated,
do not have an implementation part &
only have public components.
Interfaces primarily serve to define uniform protocols for services.
Various classes implement these services in different ways, keeping the
semantics unaltered. Interfaces contain no implementations.
Use If multiple classes have to implement a service in different ways, but
using the same method names and a uniform signature, with regular
inheritance such a method (service) will be defined in the superclass.
However if the superclass cannot be modeled suitably for inheritance,
an interface should be defined and method should be defined in the
interface same as generalization relationship with a superclass.
Classes implement interfaces as follows
The interface name is listed in the PUBLIC section of class definition.
The interface methods are implemented in the implementation part of the class.
Interface components are addressed by prefixing the interface name
followed by the interface resolution operator ~.
<interface_name>~<component_name>.
Alias names can be used to simplify accessing the interface components.
Interface components can only be accessed by using an object reference
whose class implements the interface.
Interfaces cannot be instantiated, hence an interface reference can only refer
to the instance of classes that have implemented the interface.
To perform polymorphism with interfaces, narrowing cast must be used to
copy an object reference of the class to a reference variable typed to the
interface. The interface reference can then be used to access only the
interface components in a class and not the specific class components.
Using the interface reference the interface components can be directly
accessed. No prefixing is required.
Interface references (obtained after narrowing cast) can be used to call
methods with the same name, whereby the different implementations can be
executed depending on the class whose object reference is assigned to the
interface reference.
Program
_______________
REPORT zooabap_interface.
&----
*& 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 useful.
Regards
Indrajit.
‎2007 May 23 9:42 AM
hi,
Interface, a program or group of programs that handle the passing of data from one system to another. These programs work together to export the data from one system and import into a target system, maybe these programs run once a day, maybe twice, may every hour throughout the day. It is a good way to keep the data in two or more systems in sync with each other.
Interface Programs - These program will be executed
periodically for Transaction data upload(may be
for some periodic report). So these programs are
scheduled.
Interfaces - Communication between two systems to transfer the data. It can be in a scheduled way also. Example SAP to external system and External system to SAP.
‎2007 May 23 9:51 AM
hi sudha,
i have actually requested for object-oriented concept of interface.
sorry if you have misunderstood my question, and thanks for your time.
srini
‎2007 May 23 9:53 AM
Hi Srinivasan,
I suppose u understand class and object.
In layman terms, car is a class and santro wing is an object.
So that means class is a blue print with some properties like 4 wheels, 1 stering, brake,accelerator, etc. Only the properties, no instance, no real
But Santro is a specific car which can be touched and seen and has real meaning.
Similarly, interface is a blue print with some properties, but it <b>cannot have an instance</b>. It can be implemented by any class and the properties of the instance can be used by any object of this class.
Suppose i have a class A with methods a1, a2.
I create an object ob- ob has methods a1 and a2.
There is one interface say Int1 with some methods i1 and i2.
suppose my class A implements interface Int1.
Then i create an object ob- ob will have a1, a2, i1 and i2 methods.
however, we <b>cannot create an instance of interface</b> Int1.
There is one specfic advantage of Interfaces. OOABAP does not support Multiple Inheritance.
class A class B
/
/
class Cone subclass cant inherit from 2 superclasses in ABAP. However multiple inheritance can be still be achieved by using interfaces.
Hope this helps.
Regards,
Richa
‎2007 May 23 9:56 AM
Hi,
Interfaces are independent structures that allow you to enhance the class-specific public points of contact by implementing them in classes.
->Interfaces can be defined globally in the R/3 repository or locally in ABAP program
->Can define exactly same components in Interfaces as in Classes
->Unlike classes, Interfaces do not have instances, instead they are implemented by classes
->Implemented using INTERFACES statement in the declaration part of the class
->INTERFACES statement must be included in the PUBLIC SECTION of the class
->Different classes that implement the same interface can all be addressed in the same way.
->Interfaces can also be nested.
Interfaces are the basis for polymorphism in classes, because they allow a single interface method to behave differently in different classes.
Regards,
Azaz Ali.
‎2007 May 23 10:00 AM
Hi Srinivasan,
From ABAP perspective, there is one more advantage of Interfaces.
ABAP does not support Multiple Inheritance i.e one subclass cant inherit from 2 superclasses in ABAP. However multiple inheritance can be simulated using
interfaces in ABAP i.e one class can implement 2 or more interfaces.
Regards
Indrajit.
‎2007 May 23 11:01 AM