views:

223

answers:

9

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?

+3  A: 

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".

Bozho
If you're not certain team members understand what the compiler does, then you have more serious problems...
sleske
true :) (15chrs)
Bozho
+3  A: 

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.

Cocowalla
+1  A: 

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

stacker
-1 You probably did ... implicitly
Tomas
@Tomas - yes of course, fixed it- happy now?
stacker
+11  A: 

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).

Joachim Sauer
+1 for "avoiding noise". Code is hard enough to read already...
sleske
+1  A: 

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.

denis
Or maybe you should start educating your colleague instead ;-)
Péter Török
+1  A: 

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.

Chris J
Isn't the fact that it says "extends" at the beginning of the class enough ;-)?
sleske
It would be if the constructor and the class definition were viewable at the same time which isn't always the case. I generally put all my attributes before my methods.
Chris J
+1  A: 

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

Andersson Melo
Of course, but that's a totally different situation: If you need to pass parameters, you *must* explicitly call super(parms), so it's no longer a question of coding style...
sleske
+1  A: 

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.

Gorpik
A: 

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.