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: 

INTERFACE AND EVENTS IN ABAP OO

Former Member
0 Kudos

Can some one explain me the real time need with example for use of interfaces and events in abap objects

(asking interface and events along doesnt mean that i am relating them).

<removed_by_moderator>

Pankaj Giri

Edited by: Julius Bussche on Jul 14, 2008 1:36 PM

1 ACCEPTED SOLUTION

former_member69765
Contributor
0 Kudos

Interfaces explained :

Consider a scenario where you need a service from some one. Lets say this service is Translation from German to English.

There is a Service provider - lets call him Interface_Translator.

Ideally what is desired by you - You give him german text and he gives you english text. You should really not bother how he does the translation.

Now lets think of this Interface_Translator. He has a translator (lets say a person) who knows both English and German. So he does it and gives you the result.

Lets say tomorrow ... this person leaves. So what will the Interface_Translator do ?

Lets say He employs 2 persons - One who translated German to Russian and then the other translated Russian to English.

Advantage : You are not affected by the change in the Translation service.

Why ? Because u used the interface to communicate - instead of directly contacting the person who does the translation.

Lesson : The interface will separate the actual implementation. To put it simpler - Even if the implementation logic changes .. the service that you intend will not change.

A programming example :

Right now you are calling the method that takes input as German Text and Out put as English text. This method is M1 (on the interface). This interface is implemented by class C1 that does a direct translation.

Now the situation changes... Direct transaltion is not possible... So the same interface is implemented by class C2.

Now also ... your input and out put parameters do not change. so your code does not change.

If you were using directly the class method C1 and in future the class C1 is no more... then you need to change all your code to replace C1 by C2.

Advantage : Absolutely no change in your code.

I hope it is clear !!

Varun.

5 REPLIES 5

Former Member
0 Kudos

Hi,

ABAP object doesn't directly support the concept of multiple inheritance. So we achieve it with the help of INTERFACE.

From the technical point of view INTERFACE are the superclass that cannot be instantiated, do not have any an implementation part and only have PUBLIC components. It is used for POLYMORPHISM.

Whereas, EVENT in a CLASS is a kind of BROADCASTING sender which is triggered when some/any specific thing happens (e.g New material created), EVENT is raiosed and according to the event the corresponding HANDLER(S), if any is triggered.

For further details,consult with the following links

Interface:

[http://aspalliance.com/1144_Understanding_ABAP_Object.11]

Events:

[http://help.sap.com/saphelp_nw04/helpdata/en/71/a8a77955bc11d194aa0000e8353423/frameset.htm]

[http://aspalliance.com/1144_Understanding_ABAP_Object.10]

Regards,

Anirban Bhattacharjee

former_member69765
Contributor
0 Kudos

Interfaces explained :

Consider a scenario where you need a service from some one. Lets say this service is Translation from German to English.

There is a Service provider - lets call him Interface_Translator.

Ideally what is desired by you - You give him german text and he gives you english text. You should really not bother how he does the translation.

Now lets think of this Interface_Translator. He has a translator (lets say a person) who knows both English and German. So he does it and gives you the result.

Lets say tomorrow ... this person leaves. So what will the Interface_Translator do ?

Lets say He employs 2 persons - One who translated German to Russian and then the other translated Russian to English.

Advantage : You are not affected by the change in the Translation service.

Why ? Because u used the interface to communicate - instead of directly contacting the person who does the translation.

Lesson : The interface will separate the actual implementation. To put it simpler - Even if the implementation logic changes .. the service that you intend will not change.

A programming example :

Right now you are calling the method that takes input as German Text and Out put as English text. This method is M1 (on the interface). This interface is implemented by class C1 that does a direct translation.

Now the situation changes... Direct transaltion is not possible... So the same interface is implemented by class C2.

Now also ... your input and out put parameters do not change. so your code does not change.

If you were using directly the class method C1 and in future the class C1 is no more... then you need to change all your code to replace C1 by C2.

Advantage : Absolutely no change in your code.

I hope it is clear !!

Varun.

Former Member

0 Kudos

thanks to all ,

Varun atleast now i understood the need of interfaces can u explain me the events in similar fashion

thanks in advance

regards

Pankaj Giri

0 Kudos

Events :

Technically speaking :

" Events are notifications an object receives from, or transmits to, other objects or applications. Events allow objects to perform actions whenever a specific occurrence takes place. Microsoft Windows is an event-driven operating system, events can come from other objects, applications, or user input such as mouse clicks or key presses. "

Lets say you have an ALV - An editable one ...

Lats say - Once you press some button you want some kind of validation to be done.

How to do this ?

Raise an Event - Which is handled by a method and write the validation code.

Now you might argue, that I can do it in this way : Capture the function code - and call the validate method.

Yes, in this case it can be done.. But lets say .. you change a field in the ALV and you want the validation to be done as soon as he is done with typing.

Where is the function code here ? No function code... But there is an event here - The data changed event.

So you can raise a data changed event that can be handled and will do the validation.

It is not user friendly that you ask the user to press a button (to get the function code) for validation each time he enters a data.

The events can be raised by a system, or by a program also. So in this case the data changed event is raised by a system that you can handle.

Also, Lets say on a particular action you want some code to trigger. (You can take the same example of validation code). In this case the code to trigger is in a separate class. The object of which is not available here at this moment. (This case happens very frequently).

Advantage with events : Event handlers can be in a separate class also.

e.g : In the middle of some business logic .. you encounter a error. You want to send this information to the UI (to user - in form of a pop up) and then continue with some processing.

In many cases - A direct method call to trigger the pop up is not done. Because (in ideal cases) the engine must not interact with UI directly - Because the UI could be some other application - like a windows UI but the error comes from some SAP program.

So - A event is raised from the engine that is handled in the UI and a pop up is triggered.

Here -- I would have different classes (lets say for different Operating Systems). And all these classes must register to the event ERROR raised in application.

And these different classes for different Operation systems will have different code to raise a pop-up.

Now you can imagine : If you coded a pop-up for Windows (in your application logic) .. it will not work for Mac or Linux. But of you raise a event.. that is handled separately by a different UI classes for Win, Linux or Mac they will catch this event and process accordingly.

May be I complicated this explanation .... but I couldn't think of a simpler and concrete example.

Cheers.

Varun.