cancel
Showing results for 
Search instead for 
Did you mean: 

Question regarding Class and Sub Class Constructor

AC4
Newcomer
0 Kudos

Hello!

Me and my colleagues have been studying for the SAP Back-End Developer - ABAP Cloud Certification and we have come across a question where we need some discussion regarding the right answers:

Class super has subclass sub. Which rules are valid for the sub constructor? (2 correct)

1. The method signature can be changed.
2. Events of your own instance cannot be raised before the registration of a handler in super.
3. Import parameters can only be evaluated after calling the constructor of super.
4. The constructor of super must be called before using any components of your own instance.

We know that 4. is true and 3. is wrong, but we are not sure about which one of the others is true. We have read several SAP sources and we need some input from the community.

Thanks in advance.

 

Accepted Solutions (0)

Answers (1)

Answers (1)

javier_alonso
Participant
0 Kudos

You can easily check the 1 statement.

CLASS parent DEFINITION.
  PUBLIC SECTION.
    METHODS constructor IMPORTING i_integer TYPE i.
ENDCLASS.

CLASS parent IMPLEMENTATION.
  METHOD constructor.
    WRITE / i_integer.
  ENDMETHOD.
ENDCLASS.

CLASS child DEFINITION FINAL INHERITING FROM parent.
  PUBLIC SECTION.
    METHODS constructor IMPORTING i_string TYPE string.
ENDCLASS.

CLASS child IMPLEMENTATION.
  METHOD constructor.
    super->constructor( 1 ).
    WRITE / i_string.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  NEW child( 'TEST' ).

This works fine. A subclass constructor can have a different signature. The only restriction is that you have to call the super constructor explicitly.

You can take a look at the constructor method documentation as well.