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: 

about call super->constructor in subclass

raffinkira
Participant
0 Kudos
2,684

Hi all,

For example,

The constructor of the superclass is to output something like "write: 'super'".

But in the constructor of the subclass, I need to output something like "write: 'sub'".

The problem is I have to call super->constructor in the constructor of subclass.

What should I do if I don't want to output 'super'?

1 ACCEPTED SOLUTION

matt
Active Contributor
766

The general solution to this problem is to add an optional parameter to your super class constructor. If this is set, then the super class constructor knows it has been called from the sub class.

However, you need to think carefully about your design. If the functionality of a constructor in the superclass is not always required, perhaps it shouldn't be in the super class?

18 REPLIES 18

Former Member
0 Kudos
766

from static constructor call constructor method.

matt
Active Contributor
0 Kudos
766

There is no such thing as a static constructor.

0 Kudos
766

Thank you Matthew for correcting me.

0 Kudos
766

static constrcuctor is class constructor only.

matt
Active Contributor
0 Kudos
766

You cannot call the instance constructor from the class constructor.

matt
Active Contributor
767

The general solution to this problem is to add an optional parameter to your super class constructor. If this is set, then the super class constructor knows it has been called from the sub class.

However, you need to think carefully about your design. If the functionality of a constructor in the superclass is not always required, perhaps it shouldn't be in the super class?

nagarjun_kalletla
Participant
0 Kudos
766

Hi ,

Here I Can help you in Understanding the Hierarchy of constructors


Super Class

Constructor

class_constructor

Sub class

class_Constructor

When you create  Object for Sub class

Sequence is

  • static super const
  • static sub class const
  • instance super const

Second scenario

Super Class

Constructor

class_constructor

Sub class

Constructor

Class_constructor


when Sub class has Instance Constructor

Call the super Instance in Sub class Instance Constructor

using Super->Constructor( ).

0 Kudos
766

This is factually incorrect.

The class constructor is called as soon as the class is accessed in any way. This includes, but is not limited to instantiation. If the class constructor has already been called, instantiating won't call it again.

0 Kudos
766

Hi Matthew

Class_constructor  (Static)

Constructor         (Instance) 


When ever we derive a class from super class the super class Instance Constructor must be called with  Super->constructor  in sub class Instance Constructor ( Constructor)


This is compulsory other wise you will get Syntax error.



Here is an Example of the Hierarchy


class abc definition.
public section.
methods constructor.
class-methods class_constructor.
endclass.

class abc implementation.
method constructor.
write 😕 'inside instance super const'.
endmethod.

method class_constructor.
write 😕 'inside static super const'.
endmethod.

endclass.

class pqr definition inheriting from abc.
public section.
class-methods class_constructor.
methods constructor.
endclass.

class pqr implementation.
method class_constructor.
write 😕 'inside static sub class const'.
endmethod.

method constructor.
write 😕 'inside instance of sub class '.
call method super->constructor.
endmethod.
endclass.
start-
of-selection.

data ob1 type ref to pqr.
create
object ob1.


O/P


inside static super const

inside static sub class const

Inside instance sub const

inside instance super const



0 Kudos
766

You said

When you create  Object for Sub class

Sequence is

  • static super const  <-- WRONG
  • static sub class const <-- WRONG
  • instance super const

This is simply wrong. The STATIC constructors are called as soon as the class is accessed. This may be before the object is created.

0 Kudos
766

Yes that is right we cannot call the Special Methods

but what I showed is the Sequence in which they are actually called...

If you can see my Example am not calling any Constructor directly

Just created the Object for the Derived class .

Hope I explained clearly and reached perfectly

0 Kudos
766

The sequence you have showed is not the sequence they are called, it is a possible sequence that they might be called.

The class constructors are not called whenever you instantiate an object. Your example is misleading and unclear, which is why I've called it out.

0 Kudos
766

If you can closely see my example No where I have said am calling Special Methods

When we create Object or refer the derived class  am only talking about Sequence in which they will be triggered ...

And it is the Actual sequence not possible sequence.

Just Created the Object in the code and executed   nothing else it is very clear and not misleading.

0 Kudos
766

I don't really want to go on about this, but you don't seem to understand the point.

You said:


When you create  Object for Sub class

Sequence is

  • static super const
  • static sub class const
  • instance super const

The bit I have highlighted is inaccurate and misleading, because it is not always or even usually he case.

former_member191569
Active Participant
0 Kudos
766

Hello Ming,

first of all, why do you have to call the constructor of the parent class? You can decide to call it or not. If you do not call it, the statement write: super will not get executed; of course, if you call parent constructor the code will get executed, including the statement write: super.


If you really have to call parent constructor, maybe you can change the parent constructor signature to determine some parameters that controls the output of write: super, so you can call parent constructor and not execute write: super.


Hope it helps,

David

0 Kudos
766

You are obliged to call the super constructor from the constructor in later releases of ABAP.

0 Kudos
766

You are right. I forget it

0 Kudos
766

To be completely accurate, if you access any attributes, then you must call the super constructor beforehand.