ABAP Keyword Documentation → ABAP − Reference → Declarations → Declaration Statements → Classes and Interfaces → Components in Classes and Interfaces → Methods → METHODS
METHODS - ABSTRACT, FINAL
Other versions:
7.31 | 7.40 | 7.54
Syntax
METHODS meth ... ABSTRACT|FINAL ...
Extras
Effect
The additions ABSTRACT
and FINAL
make an instance
method abstract or final. They cannot be used in interfaces, only in classes. All instance methods can
be declared as abstract except for instance constructors. The addition FINAL
can be used in all variants of the statement METHODS
.
Addition 1
... ABSTRACT
Effect
Uses the addition ABSTRACT
to define an abstract method meth
.
The addition ABSTRACT
is allowed only in abstract classes, not in interfaces.
An abstract method is not implemented in the implementation part of its class. To implement an abstract method, it must be redefined in a
specific subclass using the
addition REDEFINITION
. Private methods cannot be redefined and can therefore not be declared as abstract.
Notes
- Abstract methods can be defined in classes that are either abstract or final, but they can never be implemented and therefore are not usable.
- Methods in interfaces are abstract implicitly, because interfaces do not contain method implementations.
- With the exception of the instance constructor, concrete instance methods of a class can also call their abstract methods.
-
Static methods cannot be redefined and the addition
ABSTRACT
is not allowed in their declarations.
Example
Declaration of an abstract method of an abstract superclass and its implementation in a concrete subclass.
CLASS cls1 DEFINITION ABSTRACT.
PROTECTED SECTION.
METHODS meth ABSTRACT.
ENDCLASS.
CLASS cls2 DEFINITION INHERITING FROM cls1.
PROTECTED SECTION.
METHODS meth REDEFINITION.
ENDCLASS.
CLASS cls2 IMPLEMENTATION.
METHOD meth.
...
ENDMETHOD.
ENDCLASS.
Addition 2
... FINAL
Effect
Uses the addition FINAL
to define a final method meth
.
The addition FINAL
is allowed only in classes, not in interfaces. A final
method cannot be redefined in a subclass. In final classes, all methods are final automatically and
the addition FINAL
is not allowed. An instance constructor
constructor
is always final and FINAL
can be specified but is not mandatory.
Notes
-
Static methods cannot be redefined and the addition
FINAL
is not allowed in their declarations. -
Addition
FINAL
closes a path of an inheritance tree with regards to the possibility to redefine the method.
Example
The final method get_the_truth
of a superclass returns a value, which cannot
be changed in a subclass. Method get_opinion
can be redefined in a subclass but the displayed subclass prevents the method from being redefined in subclasses.
CLASS cls1 DEFINITION.
PUBLIC SECTION.
METHODS:
get_the_truth FINAL
RETURNING VALUE(truth) TYPE string ,
get_opinion
RETURNING VALUE(opinion) TYPE string.
ENDCLASS.
CLASS cls2 DEFINITION INHERITING FROM cls1.
PUBLIC SECTION.
METHODS get_opinion FINAL REDEFINITION.
ENDCLASS.
CLASS cls1 IMPLEMENTATION.
METHOD get_the_truth.
truth = `The final truth`.
ENDMETHOD.
METHOD get_opinion.
opinion = `Opinion of superclass`.
ENDMETHOD.
ENDCLASS.
CLASS cls2 IMPLEMENTATION.
METHOD get_opinion.
opinion = `Opinion of subclass`.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(oref) = NEW cls2( ).
cl_demo_output=>display(
|{ oref->get_the_truth( ) }\n{ oref->get_opinion( ) }| ).