Reputation: 13
We have a constructor like this:
public statusArea()
{
super();
add(pageIdentifier);
}
Why do we call super
in it?
Upvotes: 0
Views: 1353
Reputation: 2074
You only need to specify the constructor to call if:
You want to call a superclass constructor which has parameters
You want to chain to another constructor in the same class instead of the superclass constructor
Upvotes: 0
Reputation: 12328
Here you use super()
to give the parent class (ie. the class which this one extends) a chance to initialize any fields or variables, so that your sublcass can work correctly. For example, the superclass may have some fields or variables which you do not use in your subclass, but which are needed for the class to work correctly.
In this case, super()
invokes the parameterless constructor of the superclass, ie. the one which your class extends.
If you call super(...)
with parameters, then the superclass constructor with the matching parameters is called.
As others have said, if you don't call super()
yourself, the compiler will call it.
Upvotes: 1
Reputation: 15212
The purpose of the super
keyword in Java is to allow you to access the instance fields, instance methods or the constructor of a super class from which a class extends.
Since every class in Java extends from Object
implicitly, a call to super()
in a constructor for a class that does not extend
from any class means a call to the no-arg constructor in the Object
class.
Even if you don't put the super()
call explicitly in a constructor, the compiler will add one.
Upvotes: 1
Reputation: 15698
Invocation of a superclass constructor must be the first line in the subclass constructor.
The syntax for calling a superclass constructor is
super(); // the superclass no-arg constructor is called
or:
super(params);//the superclass constructor with a matching params 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.
Upvotes: 2
Reputation: 622
From Oracle docs
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.
Upvotes: 5
Reputation: 393821
super()
invokes the parameterless constructor of the super class.
In your code it makes no difference, since the compiler will add super()
implicitly if you don't. In general, you add an explicit call to the super class's constructor if you wish to call a specific constructor of the super class.
Upvotes: 3