OO ABAP – Interfaces and Polymorphism

Interfaces are simply superclasses that cannot be instantiated, do not have an implementation part, and only have public components.
This video will teach you how to define and implement interfaces to use polymorphism in ABAP.

Interfaces

  • The only real difference between interfaces and inheritance is the role they play. The programming advantages are thus the same as for inheritance.
  • Interfaces differ from regular inheritance in their area of use. In terms of programming, there are hardly any differences, however.
  • From a technical point of view, interfaces are simply superclasses that:
    • cannot be instantiated,
    • do not have an implementation part,
    • and only have public components
  • In ABAP Objects, interfaces primarily serve to define uniform interfaces for services.
  • Interfaces contain no implementations.
  • In ABAP Objects, the same components can generally be defined in interfaces and in classes.

Use of Interfaces

  • Some typical use cases of interfaces are:
    • You want to allow for the option of having multiple classes implementing a service in different
      ways, but using the same method names.

      • With regular inheritance, you would define such a method in the shared superclass. However, if you cannot model a superclass suitably for inheritance, you need to define an interface and then define this method there.
    • Classes implement interfaces as follows:
      • The interface name is listed in the definition part of the class with the INTERFACES statement. This must be in the PUBLIC SECTION, that is, interfaces can only be implemented publicly.
      • The interface methods must be implemented in the implementation part of the class.
      • The components defined in the interface can be addressed in the implementation part of the class.

Click here to download the presentation and source code for this video.