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: 

What are interfaces

Former Member
0 Kudos

sir,

plz explain me about interfaces, whare are they where r used in ABAP and explain with example.

4 REPLIES 4

Former Member
0 Kudos

Hi Sandeep,

Interfaces

Classes, their instances (objects), and access to objects using reference variables form the basics of ABAP Objects. These means already allow you to model typical business applications, such as customers, orders, order items, invoices, and so on, using objects, and to implement solutions using ABAP Objects.

However, it is often necessary for similar classes to provide similar functions that are coded differently in each class but which should provide a uniform point of contact for the user. For example, you might have two similar classes, savings account and check account, both of which have a method for calculating end of year charges. The interfaces and names of the methods are the same, but the actual implementation is different. The user of the classes and their instances must also be able to run the end of year method for all accounts, without having to worry about the actual type of each individual account.

ABAP Objects makes this possible by using interfaces. Interfaces are independent structures that you can implement in a class to extend the scope of that class. The class-specific scope of a class is defined by its components and visibility sections. The public components of a class define its public scope, since all of its attributes and method parameters can be addressed by all users.

Interfaces extend the scope of a class by adding their own components to its public section. This allows users to address different classes across different inheritance trees via a universal point of contact. Interface references allow users to address and use different classes in exactly the same way. Interfaces, along with inheritance, provide one of the pillars of polymorphism, since they allow a single method within an interface to behave differently in different classes.

Defining Interfaces

Like classes, you can define interfaces either globally in the Repository or locally in an ABAP program. For information about how to define local interfaces, refer to the Class Builder section of the ABAP Workbench Tools documentation. The definition of a local interface intf is enclosed in the statements:

INTERFACE intf.

...

ENDINTERFACE.

The definition contains the declaration for all components (attributes, methods, events) of the interface. You can define the same components in an interface as in a class. The components of interfaces do not have to be assigned individually to a visibility section, since they automatically belong to the public section of the class in which the interface is implemented. Interfaces do not have an implementation part, since their methods are implemented in the class that implements the interface.

Implementing Interfaces

Unlike classes, interfaces do not have instances. Instead, interfaces are implemented by classes. To implement an interface in a class, use the statement

INTERFACES intf.

in the declaration part of the class. This statement may only appear in the public section of the class.

When you implement an interface in a class, the components of the interface are added to the other components in the public section. A component icomp of an interface intf can be addressed as though it were a member of the class under the name intf~icomp.

The class must implement the methods of all interfaces implemented in it. The implementation part of the class must contain a method implementation for each interface method imeth:

METHOD intf~imeth.

...

ENDMETHOD.

Interfaces can be implemented by different classes. Each of these classes is extended by the same set of components. The methods of the interface can be implemented differently in each class. However, each class should keep the semantics of the interface. A method implementation should provide precisely that functionality which is required by the interface.

Interfaces allow you to use different classes in a uniform way using interface references (polymorphism). For example, interfaces that are implemented in different classes extend the public scope of each class by the same set of components. If a class does not have any class-specific public components, the interfaces define the entire public face of the class.

Interface References

Reference variables allow you to access objects (refer to Working with Objects). Instead of creating reference variables with reference to a class, you can also define them with reference to an interface. This kind of reference variable can contain references to objects of classes that implement the corresponding interface.

To define an interface reference, use the addition TYPE REF TO intf in the TYPES or DATA statement. intf must be an interface that has been declared to the program before the actual reference declaration occurs. A reference variable with the type interface reference is called an interface reference variable, or interface reference for short.

An interface reference iref allows a user to use the form iref->icomp to address all visible interface components icomp of the object to which the object reference is pointing. It allows the user to access all of the components of the object that were added to its definition by the implementation of the interface.

Addressing Objects Using Interface References

If a class class implements an interface intf, you can use the following assignment between the class reference variable crefand an interface reference irefto make the interface reference in irefpoint to the same object as the class reference in cref:

iref = cref

If a class class implements an interface intf, you do not need to create a class reference variable cref with reference to the class first in order to create an object of the class class. Instead, you can use the TYPE addition in the CREATE OBJECT statement to create an instance of the class with an interface reference variable.

CREATE OBJECT iref TYPE class.

This creates an instance of the class classto which the reference in irefpoints.

If the interface intf contains an instance attribute attr and an instance method meth, you can address the interface components as follows:

Using the class reference variable cref:

· To access an attribute attr: cref->intf~attr

· To call a method meth: CALL METHOD cref->intf~meth

Using the interface reference variable iref:

· To access an attribute attr: iref->attr

· To call a method meth: CALL METHOD iref->meth

As far as the static components of interfaces are concerned, you can only use the interface name to access constants:

To access a constant const: intf=>const

For all other static components of an interface, you can only use object references or the class class that implements the interface:

To access a static attribute attr: class=>intf~attr

To call a static method meth: CALL METHOD class=>intf~meth

Nesting Interfaces

You can nest interfaces. An interface can contain one or more interfaces as its components, and these interfaces can, in turn, themselves contain interfaces. An interface which contains another interface is called a nested or a compound interface. An interface which is contained in another interface is referred to as the component interface. An interface which does not contain any nested interfaces is called an elementary interface.

All interface components of a nested interface are on the same level. If a nested interface i3 contains the interface components i2 which are themselves nested and contain the interface components i1, then the components i1 become interface components of i3. Generally, a nested interface contains each interface component exactly once. Even if a component interface is used a second time as the component of another component interface, it still exists only once.

If you want to nest interfaces, use the statement INTERFACES in an interface definition:

INTERFACE i3.

...

INTERFACES: i1, i2 ...

...

ENDINTERFACE.

Here, the interface i3 consists of its components as well as of the interfaces i1 and i2. The components of the component interfaces are not directly visible in the nested interface. In the above definition of i3, expressions like i1comp or i2compcannot be used, with the exception of the ALIAS statement.

There are several ways how you can use the components of component interfaces:

Using Alias Names

You can use the ALIAS statement in interface definitions to assign alias names to the components of component interfaces. This makes these components visible in the interface definition.

INTERFACE i2.

...

INTERFACES i1.

ALIASES alias21 FOR i1~comp1.

...

ENDINTERFACE.

INTERFACE i3.

...

INTERFACES i2.

ALIASES alias31 FOR i2~alias21.

ALIASES alias32 FOR i2~comp2.

...

ENDINTERFACE.

Assigning Interface References

You can assign interface references typed with reference to one of the component interfaces to interface references typed with reference to a nested interface. You can then use the interface references typed with reference to a component interface to address the components of the component interfaces.

INTERFACE i2.

...

INTERFACES i1.

...

ENDINTERFACE.

INTERFACE i3.

...

INTERFACES i2.

...

ENDINTERFACE.

DATA: iref1 TYPE REF TO i1,

iref2 TYPE REF TO i2,

iref3 TYPE REF TO i3.

iref2 = iref3.

iref1 = iref2.

... iref1->comp1 ...

... iref2->comp2 ...

The following expressions are not possible:

... iref2->i1~comp1 ...

... iref3->i2~comp2 ...

Implementing Nested Interfaces in Classes

When you implement a nested interface in a class, then all interfaces contained are implemented in the class at the same level, whatever their nesting depth. The class must then implement all methods.

INTERFACE i2.

...

INTERFACES i1.

...

ENDINTERFACE.

INTERFACE i3.

...

INTERFACES i2.

...

ENDINTERFACE.

CLASS class DEFINITION.

...

INTERFACES i3.

...

ENDCLASS.

CLASS class IMPLEMENTATION.

...

METHOD i1~meth.

...

ENDMETHOD.

...

ENDCLASS.

Use:

DATA: cref TYPE REF TO class.

... cref->i1~comp1 ...

... cref->i2~comp2 ...

... cref->i3~comp3 ...

Nested expressions such as cref->i3i2comp2 or cref->i3i2i3~comp3 are not possible. The nesting hierarchy is only important when you assign interface references to each other. You can assign class references for classes which implement a nested interface to all interface references typed with reference to an interface component contained. The interface references only know the components of their interface within the class.

Interfaces and Inheritance

As far as interfaces are concerned, polymorphism is based on the fact that each class implementing an interface can implement its methods in a different way. To the outside, however, all interface components look the same. As a result, interface references can point to objects of all classes which implement the associated interface.

The concepts of interfaces and of inheritance are completely orthogonal. In the classes of an inheritance tree you can implement as many interfaces as required. However, you must pay attention to the fact that each interface can be implemented only once per inheritance tree. This ensures that each interface component has a unique name intf~icomp across the entire inheritance tree and is contained in all subclasses starting with the class that implements the interface. Interface references that can point to a class of an inheritance tree can also point to all subclasses. Once they have been implemented, interface methods are fully-fledged components of a class and can be redefined in subclasses. However, you cannot declare interface methods as abstract or final in the definition of the interface.

Reward points, if useful.

Regards,

Nitin.

Former Member
0 Kudos

Hi Dude,

Interfaces are ALE / IDOC developments. Not only development, ABAP programming for IDOCs, also IDOC customization, management. I refers EDI too.

Refer help.sap.com for inputs on these topics.

Regards,

Lakshmanan

Former Member
0 Kudos

Hello Sandeep,

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 display.

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

Interfaces are needed when we need data from other than SAP envoronment and vise versa.

Suppose aclient may send his business data through Excel sheet which are to be loaded in his SAP Tables.Then we need to use interfaces.

There are two Categogies.

1.From Presentation Layer to your ABAP Program(and then into Database table)

Presentation layer means from your sytem (from C, drives)

2.From application Server to your ABAP Program(and then into Database table)

Application Server means the server for your Sap System.

For the first case you need to use:From Presentation Layer to your ABAP Program and vise versa)

GUI_UPLOAD to upload data,

GUI_DOWNLOAD to download data

For the Second case(From application Server to your ABAP Program and vise versa)

A.OPEN DATA SET FILE NAME ON APPL.SERVER FOR INPUT ENCODING BY DEFAULT. use tranfer command to send data to application server

B. CLOSE DATA SET.

Here is the excellent information from SAP regarding Interfaces.

http://help.sap.com/saphelp_nw04/helpdata/en/fa/097241543b11d1898e0000e8322d00/content.htm

Regards

--

Sasidhar Reddy Matli.

Edited by: Sasidhar Reddy Matli on Feb 15, 2008 12:07 PM

Former Member