Google Search - Blog...........

ABAP - OOPS: Subclass Can Have Enhanced Constructor Superclass.

A subclass can modify the constructor method and add some extra functionalities. In the instance constructor method of the child class, the one for the superclass should be called first using :
CALL METHOD super->CONSTRUCTOR statement and then additional statements can be added.
Pl. note that REDEFINITION statement is not required to enhance constructors for a subclass.
------------------------------------------------------------------------------------------

REPORT YSUBOOPS18.
CLASS grandfather DEFINITION.
PUBLIC SECTION.
METHODS : CONSTRUCTOR .
ENDCLASS.

CLASS grandfather IMPLEMENTATION.
METHOD constructor.
WRITE:/5 'I am grandfather'.
skip.
ENDMETHOD.
ENDCLASS.

CLASS father DEFINITION INHERITING FROM GRANDFATHER.
public section.
METHODS : CONSTRUCTOR.
ENDCLASS.

CLASS father IMPLEMENTATION.
METHOD constructor .
call method super->constructor.
WRITE:/5 'I am father'.
skip.
ENDMETHOD.
ENDCLASS.

CLASS son DEFINITION INHERITING FROM FATHER.
public section.
METHODS : CONSTRUCTOR.
ENDCLASS.

CLASS son IMPLEMENTATION.
METHOD constructor .
call method super->constructor.
WRITE:/5 'I am son'.
skip.
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
DATA: myson type ref to son.
CREATE OBJECT: myson.

Output :

I am grandfather
I am father
I am son

No comments:

Post a Comment