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

Inheritance and constructor

former_member9607
Active Participant
2,644

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.

7 REPLIES 7
Read only

matt
Active Contributor
0 Likes
1,438

In your constructor include the statement super->constructor( ). as the first statement. Just like it says in the error.

Read only

1,438

Hi Matthew,

Thank you very much. it is workig.

But i am not getting the point why we need to do this?

Read only

Former Member
0 Likes
1,438

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

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,438

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

Read only

matt
Active Contributor
1,438

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.

Read only

Former Member
0 Likes
1,438

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

Read only

former_member9607
Active Participant
0 Likes
1,438

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    

Inheritance and Constructors

"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