Application Development 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: 

Execution of class_constructors in subclass

Former Member
0 Kudos

hi all, i'm new to abap objects but the testing the code below puzzles me. can anyone explain why if i execute WRITE b=>d, the sub class_constructor is not executed? i would expect that after executing the super c-c, the sub c-c is next. as expected, only the super c-c is executed for WRITE a=>d.

i had read, once a subclass is addressed, the c-c is executed if it hasn't been yet, after all unperformed super c-c's are executed. but why when addressing b=>d not perform it?

CLASS a DEFINITION.

PUBLIC SECTION.

CLASS-DATA d TYPE c VALUE 'D'.

CLASS-METHODS class_constructor.

ENDCLASS.

CLASS b DEFINITION INHERITING FROM a.

PUBLIC SECTION.

CLASS-METHODS class_constructor..

ENDCLASS.

CLASS a IMPLEMENTATION.

METHOD class_constructor.

WRITE:/ 'Superclass constructor'.

ENDMETHOD.

ENDCLASS.

CLASS b IMPLEMENTATION.

METHOD class_constructor.

WRITE:/ 'Subclass constructor'.

ENDMETHOD.

ENDCLASS.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

please take a look at the code below:


report  zzztest.

class a definition.
  public section.
    class-data d type c value 'D'.
    class-methods class_constructor.
endclass.                    "a DEFINITION

class b definition inheriting from a.
  public section.
    class-methods class_constructor..
endclass.                    "b DEFINITION

class a implementation.
  method class_constructor.
    write:/ 'Superclass constructor'.
  endmethod.                    "class_constructor
endclass.                    "a IMPLEMENTATION

class b implementation.
  method class_constructor.
    write:/ 'Subclass constructor'.
  endmethod.                    "class_constructor
endclass.                    "b IMPLEMENTATION

*data l_b type ref to b.

end-of-selection.

  write / 'Start'.
  write / b=>d.
  write / 'End'

The code will produce the following output:

Superclass constructor

Start

D

End

You can see that the class constructor is executed before the call to write B=>D. I think it is not exactly defined when the class_constructor is executed, but the class constructor is executed before the first object is craeted. In this case this happens before the program starts. It is not related to the write statement. It is related to the definition of the classes.

Hope this helps.

Matthias

7 REPLIES 7

Former Member
0 Kudos

Hi,

please take a look at the code below:


report  zzztest.

class a definition.
  public section.
    class-data d type c value 'D'.
    class-methods class_constructor.
endclass.                    "a DEFINITION

class b definition inheriting from a.
  public section.
    class-methods class_constructor..
endclass.                    "b DEFINITION

class a implementation.
  method class_constructor.
    write:/ 'Superclass constructor'.
  endmethod.                    "class_constructor
endclass.                    "a IMPLEMENTATION

class b implementation.
  method class_constructor.
    write:/ 'Subclass constructor'.
  endmethod.                    "class_constructor
endclass.                    "b IMPLEMENTATION

*data l_b type ref to b.

end-of-selection.

  write / 'Start'.
  write / b=>d.
  write / 'End'

The code will produce the following output:

Superclass constructor

Start

D

End

You can see that the class constructor is executed before the call to write B=>D. I think it is not exactly defined when the class_constructor is executed, but the class constructor is executed before the first object is craeted. In this case this happens before the program starts. It is not related to the write statement. It is related to the definition of the classes.

Hope this helps.

Matthias

0 Kudos

thx for the reply...

it did execute the superclass's c-c, but my question is, why did it not execute the subclass's (i.e. class b ) c-c which was to write "Subclass constructor" even though class b was addressed by using b=>d ?

0 Kudos

Hi,

b=>d is a static component. There is no need to instantiante a class b (= calling the constructor) and this means that the class to the class_constructor is deferred as well. The only thing which is for shure is that the class_constructor of B is called before the constructor of B. But up to now this is not necessary.

If you do the following the class_constructor of B is called.


report  zzztest.

class a definition.
  public section.
    class-data d type c value 'D'.
    class-methods class_constructor.
    methods constructor.
endclass.                    "a DEFINITION

class b definition inheriting from a.
  public section.
    class-methods class_constructor.
    methods constructor.
endclass.                    "b DEFINITION

class a implementation.
  method class_constructor.
    write:/ 'Superclass class constructor'.
  endmethod.                    "class_constructor
  method constructor.
    write:/ 'Superclass constructor'.
  endmethod.                    "class_constructor
endclass.                    "a IMPLEMENTATION

class b implementation.
  method class_constructor.
    write:/ 'Subclass class constructor'.
  endmethod.                    "class_constructor
  method constructor.
    super->constructor( ).
    write:/ 'Subclass constructor'.
  endmethod.                    "class_constructor

endclass.                    "b IMPLEMENTATION

data l_b type ref to b.

end-of-selection.

  write / 'Start'.
  create object l_b.
  write / b=>d.

The output is:

Superclass class constructor

Start

Subclass class constructor

Superclass constructor

Subclass constructor

D

End

Please be aware the a class_constructor is only called once for each class. The constructor is called for each object.

Hope this helps.

Regards Matthias

0 Kudos

thx...although not the answer i was looking for, nevertheless, it lead me to this....

if i add a static data in subclass b ( say CLASS-DATA e, and address that, i.e. b=>e, both the super c-c and sub c-c are called.

thx matthias, points awarded.

matt
Active Contributor
0 Kudos

>

>. I think it is not exactly defined when the class_constructor is executed,

Not correct. That would be a serious design fault. It is exactly defined. If A is the superclass of B, then the class constructor of A is guaranteed to run before B.

As appears to have been worked out, accessing an inherited component of A, that is not overridden in B, using B=>component, does not, in fact, touch B, hence the class-constructor of B isn't called. Which is quite interesting. From a design point of view, I can't see it causes a problem. Because as soon as you touch B directly, the class-constructor of B will run. And there's no way that the A-class-constructor could be (or should be) dependent on the B-class-constructor running.

matt

0 Kudos

Hi Matt,

thanks for the clarification. But this was exactly what I wanted to say. The statement you refering to should say, that the point in time in not exactly defined. Of course it is clear that the order of calls must be defined and is defined. I stated this as well in my reply. Please read my message Posted: Oct 15, 2008 5:47 PM as well. What we observe here is that the call to the class constructor is deferred until it it really necessary. This is what I mean, when I am saying that "when" (= point in time) the class constructor is called.

I hope this makes it clear. Sorry for the confusion.

Regards Matthias

matt
Active Contributor
0 Kudos

Fair enough - still, an interesting question I'd not considered before.