Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Declarations →  Declaration Statements →  Classes and Interfaces →  Components in Classes and Interfaces →  Methods →  METHODS 

METHODS - REDEFINITION

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


METHODS meth [FINAL] REDEFINITION. 

Addition

... FINAL

Effect

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 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 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.


Notes

  • Every object reference that points to an object of a subclass, independent of its static type, addresses the redefined methods. This applies in particular to 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.
  • The explicit implementation can be omitted when redefining an optional interface method declared using DEFAULT. Instead, the default behavior defined using DEFAULT applies in calls from the current subclass. This is not recommended however. The default behavior is often unwanted, particularly if an explicit implementation already exists in a preceding superclass.

Addition

... FINAL

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

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( ).