Question

This is a question about coding style and recommended practices:

As explained in the answers to the question unnecessary to put super() in constructor?, if you write a constructor for a class that is supposed to use the default (no-arg) constructor from the superclass, you may call super() at the beginning of your constructor:

public MyClass(int parm){
  super(); // leaving this out makes no difference
  // do stuff...
}

but you can also omit the call; the compiler will in both cases act as if the super() call were there.

So then, do you put the call into your constructors or not?

On the one hand, one might argue that including the super() makes things more explicit. OTOH, I always dislike writing redundant code, so personally I tend to leave it out; I do however regularly see it in code from others.

What are your experiences? Did you have problems with one or the other approach? Do you have coding guidelines which prescribe one approach?

BTW: A related question (just for reference):

Is there a reason to explicitly code a default constructor when there are no other constructors?

Was it helpful?

Solution

I don't write the super() call for the same reason I don't write unnecessary casts:

It adds noise to the code without providing any additional information (neither to the compiler nor to the developer reading my code).

OTHER TIPS

The default settings of some versions of some IDEs generate it automatically, so that's one reason you might be seeing it.

Whether to write it or not depends on who will be reading it. I prefer not to add it, but if you are not certain that team members understand what the compiler does, and how object construction proceeds, you may add it "just in case".

I don't, but the argument given to me for doing this while at university was that it makes things more explicit, so you don't 'forget' that a superclass constructor is being called.

I just don't see the need tho.

I use the super() only when the constructor super() needs parameters.

What are your experiences? Did you have problems with one or the other approach?

I never called the default constructor (explicit) and never faced any problems

I never add it and I was deeply shocked when a colleague didn't understand that there was an implicit call made to the super class so perhaps I should start doing it.

Advantage: Anyone reading your code knows that you really want to call the default constructor, you did not leave the call out by mistake.

Disadvantage: You clutter your code with unimportant information.

Usually, if a language lets you omit some instruction, it is not bad style doing so. I think it is a question of weighing the pros and cons. For instance, explicitly writing super() when your constructor has parameters and the base class also has parametered constructors available may be a good idea.

I actually put the super() call in as I find it to be a helpful memory aide. When I have written a piece of code a few months ago and return to it, the super() reminds me when I am looking at the constructor that the class I wrote inherits from something.

I try to avoid writing code where calling super() is necessary, because I prefer composition over inheritance.

If I have to derive from another class, I try to put both behind an own interface and pass one of them to the other as a dependency.

If I have a lib class, however, which I cannot give an extra dependency, the first thing I try is to use a decorator.

If this doesn't work, and I have to derive from an implementation, I think it doesn't matter whether to write super() or not, since it happens so rarely ;)

So, all in all, I think this question is pointless.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top