Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Constructors in Inheritance.

Former Member
0 Likes
332

Hi,


REPORT  Z_INHERITANCE_CONSTRUCTOR.
CLASS demo DEFINITION.
  PUBLIC SECTION.
  CLASS-METHODS main.
  ENDCLASS.
CLASS vessel DEFINITION.
  PUBLIC SECTION.
  METHODS : constructor IMPORTING i1 TYPE string
                                                            i2  TYPE string,
                     show_output.
  PROTECTED SECTION.
  DATA name TYPE string.
  DATA sex TYPE string.
  ENDCLASS.
CLASS ship DEFINITION INHERITING FROM vessel.
  ENDCLASS.
CLASS motorship DEFINITION INHERITING FROM ship.
  PUBLIC SECTION.
  METHODS : constructor IMPORTING i_name TYPE string
                                                            i_fuel_amount TYPE i.
  PRIVATE SECTION.
  DATA fuel_amount TYPE i.
  ENDCLASS.

CLASS vessel IMPLEMENTATION.
  METHOD constructor .
    name = i1.
    sex =  i2.
  ENDMETHOD. "constructor
  METHOD show_output . 
     DATA : name TYPE string,
                 sex TYPE string,
                 output type string.
                name = me->name.
                sex = me->sex.
     CONCATENATE 'The Name is : ' name '& The sex is :' sex  into output.
     MESSAGE output TYPE 'I'.
  ENDMETHOD. "show_output
  ENDCLASS.
CLASS motorship IMPLEMENTATION.
  METHOD constructor .
  super->constructor( i1 = 'Abhinab' i2 = 'M' ).
  fuel_amount = i_fuel_amount.
  ENDMETHOD. "constructor
  ENDCLASS.
CLASS demo IMPLEMENTATION.
  METHOD main .
 DATA : o_vessel TYPE REF TO vessel,
        o_ship TYPE REF TO ship,
        o_motorship TYPE REF TO motorship.
 CREATE OBJECT : o_vessel EXPORTING i1 = 'Vivien'
                                                                    i2 = 'M',
                                o_ship EXPORTING i1 = 'Sia'
                                                                i2 = 'F',
                                o_motorship EXPORTING i_fuel_amount = '1200'
                                                                          i_name = 'Eules'.
  ENDMETHOD. "main
  ENDCLASS.

  START-OF-SELECTION.
  demo=>main( ).

In my code the method show_output doesn't get implemented,but when i place the same code of show_output in the constructor method the code is getting executed.

I am not able to figure out the reason behind this.

can anybody please help me understand why is this happening??

Regards,

Abhinab Mishra

1 REPLY 1
Read only

Former Member
0 Likes
296

The show_output method was never been called.

o_vessel->show_output is required in the main method of demo.

Silly Mistake.

Regards,

Abhinab Mishra