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

Interfaces in ABAP Objects?

Former Member
0 Likes
1,286

Hi All,

Can someone explain me about interfaces in ABAP Objects with a simple example. What are Interfaces? When do we use Interfaces and how is it different from a CLASS.

<removed by moderator>

Thanks a lot.

Ibrahim

Edited by: Mike Pokraka on Aug 6, 2008 1:43 PM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,084

Hi Mohammed Ibrahim,

Interfaces are only definition of methods defined which can later be used by any class and implement those methods.

Technically, same can be achieved by defining those methods in class itself, but interface provide better flexibility in terms of reusable component (no need to again and again define common methods, of course implementation may vary), logically grouping functionalities.

Example : In drawing, TODRAW, TOCOLOR can be defined as 2 methods logically grouped together to produce a shape. How to and what to draw and color can vary depending on shape your want to produce. Class Rectangle may require very simple implementation of TODRAW interface method while Class Car may require complex implementation of TODRAW method.

Regards,

Mohaiyuddin

8 REPLIES 8
Read only

Former Member
0 Likes
1,084

Hi!

Interfaces are like superclasses ( but not the superclasses )

that cannot be instanciated. They do not have any implementation part and do not know the visibility levels of components that is all components are public.

The user generally defines the interfaces. In the interfaces, user describes the services that he wants the providers to offer. each class can now decide for itself whether it serves the interface, that is actually the services defined there.

NOTE: INTERFACE COMPONENTS CAN ONLY BE ACCESSED BY USING OBJECT REFERENCE WHOSE CLASS IMPLEMENTS THE INTERFACE.

Regards

Abhijeet

Read only

Former Member
Read only

ian_maxwell2
Active Participant
0 Likes
1,084

The best way to understand Interfaces would be independant of the ABAP language to check out information on Object oriented programming.

In a nut-shell, an interface is a contract and all classes that impliment a certain interface agree to impliment the methods of that interface. That way a program can utilize a class (for example returned from a factory pattern) without have to know the actual implimenting class just that it impliments the functionality defined by the interface.

~Ian

Read only

Former Member
0 Likes
1,084

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.

For further details,consult with the following links

Interface:

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

Regards,

Anirban

Read only

Former Member
0 Likes
1,085

Hi Mohammed Ibrahim,

Interfaces are only definition of methods defined which can later be used by any class and implement those methods.

Technically, same can be achieved by defining those methods in class itself, but interface provide better flexibility in terms of reusable component (no need to again and again define common methods, of course implementation may vary), logically grouping functionalities.

Example : In drawing, TODRAW, TOCOLOR can be defined as 2 methods logically grouped together to produce a shape. How to and what to draw and color can vary depending on shape your want to produce. Class Rectangle may require very simple implementation of TODRAW interface method while Class Car may require complex implementation of TODRAW method.

Regards,

Mohaiyuddin

Read only

former_member69765
Contributor
0 Likes
1,084

Hi..

Interfaces explained with an example :

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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 !!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`

Cheers.