‎2013 May 30 7:23 AM
Hello Friends,
I am implementing inheritance in my code.
We have a class called COMMON and another class is CREATE.
I am Inheriting COMMON in CREATE.
**********************************************************************
* CLASS COMMON DEFINITION.
**********************************************************************
CLASS COMMON DEFINITION.
ENDCLASS.
**********************************************************************
* CLASS COMMON IMPLEMENTATION.
**********************************************************************
CLASS COMMON IMPLEMENTATION.
ENDCLASS.
***********************************************************
* CLASS CREATE DEFINITION.
**********************************************************************
CLASS CREATE DEFINITION INHERITING FROM COMMON.
PUBLIC SECTION.
METHODS: constructor . "constructor
DATA : name(4) type c .
ENDCLASS.
**********************************************************************
* CLASS CREATE IMPLEMENTATION.
**********************************************************************
CLASS CREATE IMPLEMENTATION.
METHOD constructor.
name = 'Krishnakant'.
ENDMETHOD.
ENDCLASS.
I am getting a syntax error
“The constructor in super class must be called (using SUPER->Constructor) even if it does not exist, so that it can be implemented later. “
I don’t understand how to solve this. I am new to ABAB. J
Please help.
‎2013 May 30 8:40 AM
In your constructor include the statement super->constructor( ). as the first statement. Just like it says in the error.
‎2013 May 30 10:28 AM
Hi Matthew,
Thank you very much. it is workig.
But i am not getting the point why we need to do this?
‎2013 May 30 11:05 AM
Hi,
You are inheriting here COMMON. If you want to access the instance components of this COMMON class in CREATE, you need to call constructor of COMMON.
Regards,
Hari
‎2013 May 30 11:20 AM
Hello Krishna,
But i am not getting the point why we need to do this?You can draw analogy from the fact that a "child" cannot exist without the "parent".
BR,
Suhas
‎2013 May 30 1:13 PM
Because it is the law!
Because ABAP is designed that way.
Because good OO design suggests (for the reasons given by Krishnakant Joshi below), that you should always call the super-constructor before doing anything in the constructor.
‎2013 May 30 10:48 AM
Hey,
Constructor is the first method to be called in OO programming. Just call the super class constructor in your sub class constructor method.
Below is the sample code for your reference.
http://sapabap-4.blogspot.in/2013/03/sub-class-can-modify-constructor-of.html
‎2013 May 30 12:38 PM
Hello friends,
Thank you for your responses.
The answer of my question "But i am not getting the point why we need to do this?" can be found here..
http://help.sap.com/saphelp_nw70/helpdata/en/dd/4049c40f4611d3b9380000e8353423/content.htm
Look for the paragraph
"The instance constructor of a class is called by the system when you instantiate the class using CREATE OBJECT. Since a subclass contains all of the visible attributes of its superclasses, which can also be set by instance constructors, the instance constructor of a subclass has to ensure that the instance constructors of all of its superclasses are also called. To do this, the instance constructor of each subclass must contain a CALL METHOD SUPER->CONSTRUCTOR statement"
Regards,
Krishnakant Joshi