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

cannot understand "CALL METHOD super->constructor."

raffinkira
Participant
9,786

In abap oo programming, we know that constructor of each subclass must contain a "CALL METHOD SUPER->CONSTRUCTOR" statement in the implementation. But that's kind of weird. For example:

if the constructor of superclass is to output a constant like "write xxx", but I do not want to do this in the subclass, why I have to call SUPER->CONSTRUCTOR??

I really can't get it.

1 ACCEPTED SOLUTION
Read only

Former Member
5,521

Ming,

     The super class with hold generic attributes, methods or events in the hierarchy where as subclass wll hold more specific aspects. Take some realsitic examples that will make you understand better.

     Lets take the example of flight. We will have a super class named cl_flight which will have generic attributes like flight time, start time, destination etc and related processing methods. Now, the sub class would be cl_boeing which will hold flight specific details like fair, division of classes etc. When you are creating the object of cl_boeing, dont you think we have to create object of cl_flight first because it is generic and then proceed onto more specific details. Thats one of the reason why we call SUPER->CONSTRUCTOR inside the constructor of a sub class.

     Hope the above example gives you some idea of it.

~Athreya

In abap oo programming, we know that constructor of each subclass must contain a "CALL METHOD SUPER->CONSTRUCTOR" statement in the implementation. But that's kind of weird. For example:

if the constructor of superclass is to output a constant like "write xxx", but I do not want to do this in the subclass, why I have to call SUPER->CONSTRUCTOR??

I really can't get it.

12 REPLIES 12
Read only

Former Member
0 Likes
5,521

this is required if there are any intializations to be done..in dynamic programming we dont know, which class method is getting executed. or if any of sub class method calls super class method, then constructor can feed defaults

Read only

Former Member
5,522

Ming,

     The super class with hold generic attributes, methods or events in the hierarchy where as subclass wll hold more specific aspects. Take some realsitic examples that will make you understand better.

     Lets take the example of flight. We will have a super class named cl_flight which will have generic attributes like flight time, start time, destination etc and related processing methods. Now, the sub class would be cl_boeing which will hold flight specific details like fair, division of classes etc. When you are creating the object of cl_boeing, dont you think we have to create object of cl_flight first because it is generic and then proceed onto more specific details. Thats one of the reason why we call SUPER->CONSTRUCTOR inside the constructor of a sub class.

     Hope the above example gives you some idea of it.

~Athreya

Read only

0 Likes
5,521

Yes, I know that if you call SUPER->CONSTRUCTOR, that means you do not need to rewrite the whole implementation in superclass's constructor. You can just write something else as supplementary in the subclass's constructor.

In that case, it works well.

But the problem is, what if I do not want the stuff in superclass's constructor anymore when I am rewrite the subclass's construtor?

Do I only have two choices:

1, change the stuff in superclass's construtor.

2, do not use constructor, use ordinary methods.

Read only

matt
Active Contributor
0 Likes
5,521

You only have choice 1. If you have no constructor in your subclass, the super-constructor is called automatically. It seems that in your case the super-constructor has been badly defined. Or it's just one of those things where you want it to be more general than it is.

What I would do is add an additional parameter to the super-constructor,i_nodisplay type abap_bool default abap_false. Change the implementation of the super-constructor to

IF i_nodisplay EQ abap_false.

... the code you want to skip.

ENDIF.

Then in the subclass constructor use

super->constructor( i_nodisplay = abap_true ...<other parameters> ).

In this way, you've refactored the class so that it works as expected for everything that already uses it, and the way you want it from your subclass.

Read only

0 Likes
5,521

Thank you for your reply. Now I am much clearer.

But what you have done makes the program more complicated logically, it seems not convenient that abap does not support method overload.

Read only

matt
Active Contributor
0 Likes
5,521

There are some annoyances. This one was a design decision.

Be aware that if you call a method from the super-constructor, it is always the implementation in the superclass that is called - even if the method is overridden in the subclass.

That's why I generally use the factory pattern for instantiation.

Read only

0 Likes
5,521

Hello,

I know this thread has already been answered and it is very old.

But, I think there is something still missing which is very vital -

Here it is:

If your "super class" has something that is NOT required by your "subclass" then there is a flaw in the modelling itself.  There cannot be exceptions like - "All the sub classes of this super class need 'that functionality' except 'one sub class' "  -- that's wrong.

In such cases where there are exceptions, you need to check if your subclass is really a subclass of the super class.  If there are exceptions, then, it is most certainly NOT.

I think it is a very bad idea to conditionally omit or include some functionality in the "super class constructor".  If you are doing that, you may probably want to push that part of the code to some "sub class" which needs it there by relieving all other sub classes of the "pain" of having to go through that "conditional construction".

As Athreya Hegde already noted, ALL the Boeings are "FLIGHTS".  So, if the "FLIGHT" has 2 wings and Boeing is a sub class, then that too has 2 wings by default.  If you want "Boeing" to have 4 wings, you need to do that part of the construction in the "Boeing" class and not conditionally in the "FLIGHT" class.


I hope this helpful.

Thank you

Dhananjay

Read only

matt
Active Contributor
0 Likes
5,521

I agree that coding exceptions in the superclass constructor indicates a broken design, however, I think:


There cannot be exceptions like - "All the sub classes of this super class need 'that functionality' except 'one sub class' "  -- that's wrong.


s a mis-statment. With every method.


1. Make the method final - all subclasses must use that functionality

2. Make the method abstract - all subclasses must define their own functionality

3. Give the method a default functionality - any subclass may override the method

Now, with option 3, you're not obliged to call the super->method( ). Perhaps every class but one uses the default. The one classes that doesn't, that overrides the method, exactly fits the paradigm "All the subclasses of this super class need that functionality - except this one that's defined it's own".

Read only

0 Likes
5,521

Hello Matthew,

Yes, if you take my statement out of the context of the question, then it sounds wrong.  I agree with you.

I have also mentioned in my comment that

I think it is a very bad idea to conditionally omit or include some functionality in the "super class constructor".  If you are doing that, you may probably want to push that part of the code to some "sub class" which needs it there by relieving all other sub classes of the "pain" of having to go through that "conditional construction

- if some behavior is not applicable to "all" the sub classes, then it should be pushed down the inheritance hierarchy and let the sub classes decide whether they want it or not.

That is what you say in 'statement 3' - by default, behavior of the super class is inherited to the sub class - all the sub classes.  If a sub class chooses to have an exception, it has to implement it on its own and not expect the super class to do it.

Are we not saying the same thing?

But, yes, you have put it in more clear way   Thank you for that.

Regards

Dhananjay

Read only

arindam_m
Active Contributor
0 Likes
5,521

The SUB-CLASS as the name suggests has to be SUB to a SUPER class meaning the the very basic definition of the SUPER class has to be there. And the best way to do that is to have a constructor in the SUB class that MIMICS the constuctor method in SUPER class. There can be additional stuff(Methods) that you can do in SUB-class but its basic definition cannot be different from SUPER class.

If you do not want the constructor to be same you can build a different class not keep it as a SUBclass this will give you the freedom to define your own constructors. And then you can mimic or copy other methods from your reference class that you want it to be same.

Hope it helps your understanding

Cheers

Read only

matt
Active Contributor
0 Likes
5,521

It is part of the ABAP Objects language that the super constructor must be called from the subclass constructor. It is part of the philosopy to ensure that all that needs to be initialised, is initialised. If the super-constructor is doing something you don't like, then you have to make changes there.

Note: unlike other inherited classes, constructors of subclasses do not have to have the same parameters as the superclass consturctor.

Read only

Former Member
0 Likes
5,521

Hi, I dont understand why you would like to use constructor for popping out write XXX any ways constructors are meant to initialise instance variables. okay now suppose u have a class A u will have some variables A1, A2 and then when the child class B declares A as it's super class u have child class specific variables say  B1, B2.. The ABAP system will in case of the creation of thye child class B first call the constructors of the parent class it will alwas do it that way...

so if it so happens that u have declared a constructor method in child class B then first the constructor of the parent class A will be executed....and then execute the code component specific to ur own implemented version of the child class.

Now funny thing is if u do not declare the constructor class for the child class, the super->constructor call is irrelevant u  dont do it as u dont have a constructor,

now say u declare a constructor in class B and u want to initialise all variables ur way do not call super->constructor do it in ur implementaion but if in caseu only want to give implementation specific to B1 and B2 and let teh system init A1 and A2 as per the implementation in super class constructor then u write some thing like say for exampe.

Class A...Constructor_A{ A1 =10. A2 =20. }

Class B -> Superclass Class A.

Constructor_B for Class B{ super->constructor. " will make A1 =10, A2 =20.

                                   B1 = 100. B2 =200.}

Also possible Constructor_B{ A1 =100.

                                           A2 =200.

                                           B1 = 10. B2 =20. }