METHODS - REDEFINITION
Short Reference
ABAP Syntax_5 METHODS meth [FINAL] REDEFINITION.
ABAP_ADDITION:
... FINAL
What does it do? This statement is possible only in subclasses; it
redefines an inherited instance method meth . As an effect, the
method meth must be re-implemented in the implementation section
of the subclass. The new implementation in the current class shadows 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 .
With the exception of the instance
constructor , meth can be specified as any non-final instance
method declared in the public or
protected
visibility area 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 method kind (
general or
functional instance method ,
event handler ) are not changed in a
redefinition.
Latest notes: Every object reference that points to an object of a
subclass, independent of its static type, addresses the redefined
methods. This especially applies for the
self reference me- .
When an instance constructor
of a superclass is executed as part of the creation of an object, the
method implementations of the superclass are always called and not the
redefined methods of the subclass. Specifying the self-reference
me- does not have any affect at this time.
In the redefinition of a method of an interface, an alias name of the
class defined using ALIASES can be specified
for meth .
ABAP_ADDITION ... FINAL
The redefinition is valid for the subclasses of the redefined class
until it is again redefined. A method can be redefined repeatedly along
a path in the inheritance tree until the addition FINAL is used
in the redefinition. Then the method is final staring with the current
class and can no longer be redefined in its subclasses.
Example ABAP Coding In this example, the method m1 of superclass
c1 is redefined in subclass c2 , where the original
implementation is called with 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( ).
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved