In Java starting from JDK 8 it is possible to define a default implementation of a method in an interface with the default
keyword at the beginning of the method signature, mainly to enable to add new functionality to the interfaces and ensure compatibility with code written for older versions of those interfaces.
In addition to default methods, it is also possible to define static
methods in an interface, which makes it easier to organize helper methods in libraries and keep static methods specific to an interface in the same interface rather than in a separate class.
Is something like this possible in ABAP?
As I don't have a lot of experience with ABAP OO, I did not find an exact answer.
Unfortunately this is not possible in ABAP.
An interface contains only definitions and the actual implementation must be provided in a class that implements the interface. All the methods of an interface are abstract (i.e. they are fully defined including their signatures but not implemented).
Unlike classes, an interface does not have an implementation section at all. All of the components are in PUBLIC section by default, there no visibility sections and all components of the interface are visible.
An interface can have the following components:
TYPES
, DATA
, CLASS-DATA
, CONSTANTS
: data types and data objects
METHODS
, CLASS-METHODS
, EVENTS
, CLASS-EVENTS
: methods and events
interfaces provide fewer variants than classes for declarations of methods: no constructors, test methods, or AMDP function implementations are allowed
INTERFACES
: inclusion of component interfaces;
ALIASES
: alias names for components of interfaces
As we can see CLASS-METHODS
indeed allows to define static method declarations, and the static methods are just NOT instance specific. It means that when a class implements such an interface, the methods defined as static do not require an instance to access / call them and they rather can be called on the class itself like class_name=>interface_name~static_method_name
.
The purpose of declaring a method as static in an interface definition is actually to create an agreement that a particular method will not depend on an instance to work.
There are, however, also PUBLIC
and DEFAULT
keywords which might be used inside interface definition and which could at first lead to a confusion.
Interface definition looks like:
INTERFACE intf [PUBLIC].
[components]
ENDINTERFACE.
Addition PUBLIC
makes the interface a global interface of the class library and can only be used for the global interface of an interface pool and is created by class builder when a global interface is created.
Keyword DEFAULT
is available since Release 7.40, SP08
METHODS meth [ABSTRACT|FINAL] | [DEFAULT IGNORE|FAIL] ...
CLASS-METHODS meth [DEFAULT IGNORE|FAIL] ...
The addition DEFAULT
of the statements METHODS
and CLASS-METHODS
can be used to make these methods optional. An optional interface method does not need to be implemented explicitly in a class when an interface is implemented. Instead, a default behavior is specified for calls of non-implemented methods in the definition:
DEFAULT IGNORE
- the behavior is the same as when it is implemented with an empty body
DEFAULT FAIL
- raise an exception of the class CX_SY_DYN_CALL_ILLEGAL_METHOD
in case if not handled, the runtime error CALL_METHOD_NOT_IMPLEMENTED
is raised
You can, of course, employ the inheritance to have a basic methods' implementations, creating a (abstract) class with implemented methods and if necessary reimplement / override them in subclasses (keyword METHODS .. REDEFINITION
).