SAP NetWeaver AS ABAP Release 750, ©Copyright 2016 SAP AG. All rights reserved.
ABAP - Keyword Documentation → ABAP - Reference → Declarations → Declaration Statements → Classes and Interfaces → Components in Classes and Interfaces → Methods → METHODS →
METHODS - REDEFINITION
Syntax
METHODS meth [FINAL] REDEFINITION.
Addition:
Effect
This statement is possible only in subclasses and redefines an inherited instance method meth. It dictates that the method meth must be reimplemented in the implementation part of the subclass. The new implementation in the current class obscures the implementation of the superclass. The redefined method accesses the private components of the redefined class and not any private components of the same name in the superclass. In the redefined method, the implementation of the direct superclass can be called using super->meth. The redefinition is valid for the subclasses of the redefined class until the method is redefined again.
With the exception of the instance constructor, meth can be specified as any non-final instance method declared in the
public or
protected
visibility section
of a superclass of the current class. In particular, meth can be an abstract method of an abstract superclass. The
redefinition must happen in the same visibility section as the method declaration. The interface and the category of the method (a
general or
functional
instance method or
event handler) are not changed in a redefinition. Private methods in superclasses cannot be redefined.
Notes
... FINAL
A method can be redefined repeatedly along a path in the inheritance tree until the addition
FINAL is used in the redefinition. The method is then final starting with the current class and can no longer be redefined in its subclasses.
Example
In this example, the method m1 of superclass c1 is redefined in subclass c2, where the original implementation is called using super->m1. Both methods use the private attribute a1 of the respective class. When calling using the reference variable oref, which has the static type c1 and the dynamic type c2, the redefined method is executed.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS m1 IMPORTING p1 TYPE string.
PRIVATE SECTION.
DATA a1 TYPE string VALUE `c1: `.
ENDCLASS.
CLASS c2 DEFINITION INHERITING FROM c1.
PUBLIC SECTION.
METHODS m1 REDEFINITION.
PRIVATE SECTION.
DATA a1 TYPE string VALUE `c2: `.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1.
a1 = a1 && p1.
cl_demo_output=>write_data( a1 ).
ENDMETHOD.
ENDCLASS.
CLASS c2 IMPLEMENTATION.
METHOD m1.
super->m1( p1 ).
a1 = a1 && p1.
cl_demo_output=>write_data( a1 ).
ENDMETHOD.
ENDCLASS.
DATA oref TYPE REF TO c1.
START-OF-SELECTION.
oref = NEW c2( ).
oref->m1( `...` ).
cl_demo_output=>display( ).