13

When I write a constructor in my java class, I typically do not invoke super() in there. When I generate the constructor from eclipse source code editor, why does it always add the super() in there?

Am I wrong for not adding this by default in the constructors I write? Any thing wrong with leaving the super() call in the constructor if I decide to use eclipse code generator?

arshajii
  • 127,459
  • 24
  • 238
  • 287
Horse Voice
  • 8,138
  • 15
  • 69
  • 120

3 Answers3

12

As @Kon correctly points out, there is an implicit call to the default super constructor anyway (this can be easily verified by checking the bytecode with javap -c). If you don't want Eclipse to make it explicit, simply check the "Omit call to default constructor super()" checkbox at the bottom of the constructor creation GUI.

enter image description here


Am I wrong for not adding this by default in the constructors I write?

No, as long as long as you're referring to the default super constructor call super(). If the super constructor takes parameters, for example, then you need to make the call explicit.

Any thing wrong with leaving the super() call in the constructor if I decide to use eclipse code generator?

No, not at all.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • Thank you for the response. When you said `this can be easily verified by checking the bytecode with javap -c`. I don't understand what that means. Can you elaborate what javap -c does? thank you – Horse Voice May 14 '14 at 02:16
  • @TazMan Java code compiles to bytecode which is stored in `.class` files. You can see the bytecode in a readable form by running `javap -c MyClass` on the command line. This isn't really pertinent to your question, I just added it as an aside. – arshajii May 14 '14 at 02:19
7

As @Kon mentioned in his comment, an empty constructor in Java contains an implicit call to the superclass constructor.

Moreover, a non-empty constructor without an explicit call to super() will have an implicit call at the top.

The only time when it is wrong to leave the super() call there is if you intend to call a different superclass constructor yourself, with parameters.

See this question for more details.

Update: Consider the following code, which illustrates the scenario where it is wrong to leave the super() generated by eclipse.

public class Foo{
    public Foo(int a, int b) {
        System.out.println("Foo constructor with-args is called");
    }
    public Foo() {
        System.out.println("Foo with no-args is called");
    }
}
class Bar extends Foo {
    public Bar() {
        // Implicit call to super()
        super(); 

        // Explicit call to super(a,b);
        // This will not compile unless the call above has been removed.
        super(1,2);
    }
}
Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • thank you. I don't understand what you mean by: `The only time when it is wrong to leave the super() call there is if you intend to call a different superclass constructor yourself, with parameters.` When would that be the case? A call to `super(1,2,3)` for example passing in those arguments would call constructor of parent class. No other different class right? – Horse Voice May 14 '14 at 02:19
  • @TazMan, A call to `super()` is always a call to the parent class's constructor. However, in the event that the parent class has a constructor which takes arguments, and you want to invoke *that* constructor, you would have to remove `super()` and replace it with `super(arg1,arg2,arg3)`. – merlin2011 May 14 '14 at 02:21
  • @TazMan, I added an example of where `it is wrong to leave the super() call there`. – merlin2011 May 14 '14 at 02:26
2

Nothing wrong it's just a coding style preference. Some people like to write code that is implicit and some don't.

If you don't call super constructor from your child class constructor compiler will place call to super's default constructor in byte code for you. See this SO question as well

Community
  • 1
  • 1
vkg
  • 1,839
  • 14
  • 15