Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you put a super() call a the beginning of your constructors?

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?

like image 304
sleske Avatar asked Apr 26 '10 11:04

sleske


1 Answers

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

like image 118
Joachim Sauer Avatar answered Sep 21 '22 23:09

Joachim Sauer