banner ads

Sub Class Inherits Constructor of Super Class.

Constructor of Super class is Inherited by its Sub classes. Hence super class needs not to be instantiated in the data processing to fetch the functionality of the constructor.

We have derived a program where we have defined a parent class and a constructor in the public section. Then we implement that constructor in the implementation portion. Next we define child class inheriting from parent class. Now in the start of selection when we create the object of child class then the constructor method is called automatically and we get the functionality implemented in that constructor. Hence we are not creating any object for the parent class but we can get its constructor. Here we are getting the constructor of super class by creating the object of sub class.


REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS test DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS test DEFINITION.
  PUBLIC SECTION.
    DATA v_test TYPE char40.
    METHODS constructor.
ENDCLASS.                    "test DEFINITION

*----------------------------------------------------------------------*
*       CLASS test IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS test IMPLEMENTATION.
  METHOD constructor.
    v_test = 'SAP ABAP Object Oriented'.
    WRITE: / v_test.
  ENDMETHOD.                    "constructor
ENDCLASS.                    "test IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM test.
ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
ENDCLASS.                    "child IMPLEMENTATION

START-OF-SELECTION.  DATA obj TYPE REF TO child.
  CREATE OBJECT obj.

Below is the output:

No comments