2014 Jun 18 7:22 AM
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'?
2014 Jun 18 8:05 AM
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?
2014 Jun 18 7:46 AM
2014 Jun 18 8:05 AM
2014 Jun 18 8:12 AM
2014 Jun 18 8:16 AM
2014 Jun 18 10:46 AM
You cannot call the instance constructor from the class constructor.
2014 Jun 18 8:05 AM
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?
2014 Jun 18 8:25 AM
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
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( ).
2014 Jun 18 10:49 AM
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.
2014 Jun 18 11:14 AM
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
2014 Jun 18 11:21 AM
You said
When you create Object for Sub class
Sequence is
This is simply wrong. The STATIC constructors are called as soon as the class is accessed. This may be before the object is created.
2014 Jun 18 12:00 PM
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
2014 Jun 18 12:25 PM
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.
2014 Jun 18 1:14 PM
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.
2014 Jun 18 1:25 PM
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.
2014 Jun 18 10:53 AM
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
2014 Jun 18 11:18 AM
You are obliged to call the super constructor from the constructor in later releases of ABAP.
2014 Jun 18 11:24 AM
2014 Jun 18 11:45 AM
To be completely accurate, if you access any attributes, then you must call the super constructor beforehand.