SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Object Oriented Programming in ABAP - Subclass can have enhanced constructor than its superclass.

Object Oriented Programming in ABAP - Subclass can have enhanced constructor than its 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.

Please note that REDEFINITION statement is not required to enhance constructors for a subclass.

 

This program contains three classes:-

  • GRANDFATHER at the top of the node with its own constructor method
  • FATHER, subclass of GRANDFATHER with enhanced constructor
  • SON, subclass of FATHER with its own enhanced constructor.

An object is created finally from the class SON, which triggers the constructor methods in the order: GRANDFATHER->FATHER->SON.

 



REPORT zstp_oo_abap.

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 -

Object Oriented Programming in ABAP - Subclass can have enhanced constructor than its superclass.

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in