MichaelB.
7/28/2018 - 10:48 AM

Call the static constructor of the superclass

The same principles apply for static constructors as they do for instance constructors. Declaring a static constructor in a subclass - redefines the static constructor of its superclass. You do not use the REDEFINITION addition. When redefining static constructors in a subclass, you do not have to call the static constructor of the superclass. This is all taken care of by the runtime environment automatically to ensure the static constructors are called in the correct sequence.

Remember:

  • The first time a class is addressed in our program the static constructor is executed
  • The same applies for a subclass
  • Inheritance ensures static constructors of a subclass and superclass are called the first time a subclass is referenced by the system
  • The system will search through the inheritance hierarchy to execute the next highest superclass until the top level is reached - then execute it first
CLASS ford DEFINITION.
  PUBLIC SECTION.

    CLASS-METHODS class_constructor.      "Static Constructor

    METHODS constructor
      IMPORTING
        p_model TYPE string.

  PROTECTED SECTION.
    DATA model TYPE string.
    CLASS-DATA: carlog TYPE c LENGTH 40.

ENDCLASS.

CLASS mercedes DEFINITION INHERITING FROM ford.

  PUBLIC SECTION.
    CLASS-METHODS class_constructor.      "Static Constructor

ENDCLASS.


CLASS audi DEFINITION INHERITING FROM mercedes.

  PUBLIC SECTION.
    CLASS-METHODS class_constructor.      "Static Constructor

    METHODS constructor
      IMPORTING
        p_model  TYPE string
        p_wheels TYPE i.

  PROTECTED SECTION.
    DATA wheels TYPE i.

ENDCLASS.

* IMPLEMENTATION

CLASS ford IMPLEMENTATION.
  METHOD class_constructor.
    carlog = 'FORD class constructor has been used'.
    WRITE: / carlog.
  ENDMETHOD.


  METHOD constructor.
    model = p_model.
  ENDMETHOD.
ENDCLASS.

CLASS mercedes IMPLEMENTATION.       " Redefine the Ford Class Constructor
  METHOD class_constructor.
    carlog = 'MERCEDES class constructor has been used'.
    WRITE: / carlog.
  ENDMETHOD.
ENDCLASS.

CLASS audi IMPLEMENTATION.
  METHOD class_constructor.       " Redefine the Mercedes Class Constructor
    carlog = 'AUDI class constructor has been used'.
    WRITE: / carlog.
  ENDMETHOD.

  METHOD constructor.
    CALL METHOD super->constructor
      EXPORTING
        p_model = p_model.
    wheels = p_wheels.
  ENDMETHOD.
ENDCLASS.



* Our program starts here.

DATA: my_ford     TYPE REF TO ford,
      my_mercedes TYPE REF TO mercedes,
      my_audi     TYPE REF TO audi.

START-OF-SELECTION.

CREATE OBJECT: my_audi       EXPORTING p_model = 'A4'         " SUBCLASS - INTERFACE REDEFINED
                                       p_wheels = 4,
               my_ford       EXPORTING p_model = 'FOCUS',     " SUPERCLASS
               my_mercedes   EXPORTING p_model = 'C-CLASS'.   " SUBCLASS - NO REDEFINITION


ULINE.