When do you call super() in Java? I see it in some constructors of the derived class, but isn\'t the constructors for each of the parent class called automatically? Why woul
super() is implicit when no other class/superclass constructor is called.
There is no need to call super().
From Accessing Superclass Members :
With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
I wanted to provide some information that hasn't been mentioned so far. If you use a call to this(...)
in a constructor, then you can't have a call to super(...);
This also includes Java's automatic insertion of the parameterless call to super();
The following example illustrates this point, including explanatory comments:
public class B extends A {
private int x;
public B() {
// Java doesn't call super(); here, because
// of the call to this(...); below.
// You can't call super(...) here either,
// for the same reason.
this(42); // Calls public B(int x) below.
}
public B(int x) {
// Java does call super(); here.
// You can call super(...) here, if you want/need to.
// The net result of calling new B() above is that
// super(...) for class A only gets called once.
this.x = x;
}
}
Although super()
does nothing functionally for the compiler (the superclass default constructor is called automatically), it certainly does a lot for me. It says to me: "Do not remove the empty constructor. It's there for a reason".
A perfect example is where you create Spring managed beans or JPA Entities and you've created a parameterized constructor.
@Entity
public class Portfolio {
...
public Portfolio() {
super(); // This says to me: DON'T DELETE!
}
/**
* Because there is now a parameterised constructor (me),
* the default constructor no longer exists. This means
* that for this example, you need an empty constructor in place (above)
**/
public Portfolio(String name) {
this.name = name;
}
}
If you provide a class like this:
public class Foo
{
}
or this:
public class Foo()
{
public Foo()
{
}
}
the compiler will generate code for this:
public class Foo()
{
public Foo()
{
super();
}
}
So, strictly speaking, the call to "super()" is always there.
In practice you should only call "super(...)" where there are parameters you want to pass to the parent constructor.
It isn't wrong to call "super()" (with no parameters) but people will laugh at you :-)
As has been said, if your constructor does not explicitly call super() with or without some argument, java will automatically call the default constructor of the superclass.
However, explicitly calling super() is just that--explicit. If you know the superclass's constructor does something significant, it's a useful reminder to whomever is maintaining your code that super() is being called first (and may have side effects). This might even be a useful spot to put a breakpoint while debugging.