Use of Interfaces
In ABAP Objects, the same components can generally be defined in interfaces and classes. To recognize the semantic differences from regular inheritance, you can concentrate on the following use cases.
For example, you want to allow multiple classes to implement a service in different ways, but using the same method names and a uniform signature. With regular inheritance, you would define such a method in the shared superclass. However, if you cannot model a superclass for inheritance suitably, you need to define an interface and then define the method in the interface. Therefore, you can compare this case with a generalization relationship within a superclass.
Interfaces differ from regular inheritance in their areas of use. However, in terms of programming, there are hardly any differences between interfaces and regular inheritance. Interfaces can be seen as superclasses that cannot be instantiated, do not contain implementations, and have only public components. You can simulate multiple inheritances using interfaces.
In ABAP Objects, interfaces primarily serve to define uniform interface protocols for services. Various classes can implement these services in different ways, but you need to keep the same semantics. Therefore, interfaces contain no implementations.
Definition of Interfaces
Compared to regular inheritance, the distribution of roles in interfaces is sometimes different. The user generally defines the interfaces. In these interfaces, the user describes both technically and semantically the services that the user wants the providers to offer. Each class can now decide for itself whether it serves the interface, that is, whether it actually offers the services defined in the interface. Therefore, this case is similar to a specialization relationship with a subclass.
As with regular inheritance, access to these services defined in the interface is then usually generic, that is, access to the services uses a reference that is typed on the interface. As in inheritance, polymorphism can also be implemented in interfaces.
Generalization and Specialization Relationships Using Interfaces
In ABAP Objects, you can define the same components in an interface as you can in classes. However, interfaces do not know the visibility levels of their components, that is, all the components of an interface are public.
Classes implement interfaces in the following ways:
Interface components are distinguished from the other components in the implementing class by prefixing the interface name followed by a tilde (~), which is the interface resolution operator.
interface_name~component_name
Access to Interface Components
You can access interface components only by using an object reference whose class implements the interface. You use the interface resolution operator (~) to access the interface components in the implementation part of the class.
To simplify access to interface components, you can use alias names. These alias names can appear in the definition part of a class. The use of alias names is subject to the visibility restriction of the defining class.
An alias for an interface method is defined as follows:
ALIASES a_1 FOR lif_1~method_1.
The interface method lif_1~method_1 can be addressed with the shorter form ref->a_1.