Should super() be called implicitly or explicitly? - java

I learned that when constructing an object, super() will be called, whether you write it in the constructor or not. But I noticed that in some code, the super() method is called explicitly. Should I be calling super() implicitly or explicitly in a constructor? What's the difference?

Should I call the super() method implicitly or explicitly in a constructor? What's the difference?
There is no semantic difference between calling super() or not, and no performance difference either.
This is purely a cosmetic issue. Make up your own mind ... or go with what (if anything) your project's adopted style guide says.
On the other hand, if the super you need to call has parameters, then it does matter that you call it explicitly. And if the superclass doesn't have a no-args constructor, then you can't use super(), either explicitly or implicitly.

There is no difference if the discussion is around no-argument super() constructor.
Java calls super() implicitly for all your base class constructors if not called explicitly. Remember Java only calls super class no-argument constructor always.
In some cases, as a developer you may want to call argument-based super constructor. In those cases, you have to explicitly call argument-based super constructor eg: super(arg1, arg2, ...)
IMO, avoid calling no-argument super() constructor since it neither have any impact on logic nor improves readability.

Related

When must an explicit call to super constructor be used?

I am learning about super() constructor and I ran into this statement:
Since the inherited instance variables should be initialized, and the base class constructor is designed to do that, then an explicit call to super() should always be used.
What does this statement mean?
Except for the fact that implicit call can result in an error if the base class has not defined a no-argument constructor?
I think the quoted paragraph is a little bit misleading because you only need to explicitly call a parametrised super() if you are not happy with the unparametrised implicit super() call.
You would also have to do a parametrised super() call if the base class only has parametrised constructors.
This sentence you quoted doesn't explicitly say that you need to have 0 argument constructor in your class. The point of the sentence is that all the variables that are part of parent class should be initialized and you initialize those variables with a constructor, therefore you should call parent class constructor inside of your child class constructor. Doesn't matter what is the number of arguments of parent class constructor.

Calling overloaded contructor of current class or super class

In java, can a contructor in a class call an overloaded constructor of its superclass (say if we wanted to make that call explicitly and deliberately). I know that a constructor in a class makes implicit call to no-arg default constructor of superclass ( with super (); ) . but suppose I make a call to an overloaded superclass constructor (say super(String s); ) , then my question is, Is this possible? And if this is possible, then is a call to super() still made ahead of super(String s)or not and what are its implications? Also can two constructors from same class one no-arg and one overloaded call each other? Will they be caught in a loop if they do so?
You can get the answer in its official tutorial:
https://docs.oracle.com/javase/tutorial/java/IandI/super.html
Read this specifically:
The syntax for calling a superclass constructor is
super();
or:
super(parameter list);
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. If a subclass constructor invokes a
constructor of its superclass, either explicitly or implicitly, you
might think that there will be a whole chain of constructors called,
all the way back to the constructor of Object. In fact, this is the
case. It is called constructor chaining, and you need to be aware of
it when there is a long line of class descent.
So to answer your question:
Yes, it is possible, and doable. When you explicitly invoke super constructor with arguments, then only that constructor is invoked.
And, make sure the super constructor invocation is the first line in your constructor, otherwise a compiler error will show.
*****EDITED*****
And, you invoke one specific super class constructor, implicitly or explicit, only. When that superclass constructor is called no other superclass constructors are called, unless it is called within your called superclass constructor. Meanwhile, it is not possible to compile if in the same class multiple constructors call each other recursively - it will be rejected by your compiler.
Hope this helps.

What for we need super() in ArrayList constructor?

this code from ArrayList source:
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
this code from AbstractList source:
protected AbstractList() {
}
What does super() do?
In general, super will invoke its parent constructor with matching arguments. In this case, because AbstractList has an implicit no-arg constructor, we use super() with no arguments to invoke the implicit no-arg constructor.
As to why - there's really no reason to do it in this case, if there's no requirement to set fields in the parent class. This may have been a case of an older style of programming.
It does no harm, and it's self-documenting; it's explicit in that it calls its parent's constructor. Although, I will note that the Javadoc for that constructor calls out its invocation usefulness:
/**
* Sole constructor. (For invocation by subclass constructors, typically
* implicit.)
*/
You're more likely to see implicit invocations of that constructor than you are explicit.
super() call parent constructor.
super() is never needed, however it is not a bad habit to write it, because it is good for readability - it reminds you that parent constructor is called.
See this question When do you need to explicitly call a superclass constructor?

Calling superclass constructor with super or just setTitle?

What is the recommended method of setting the title with setTitle("Title")or super("Title") while extending javax.swing.JFrame in terms of performance?
If you grepcode JFrame (in OpenJDK 6-b14), and dig a bit, you see that the constructor JFrame() calls the constructor Frame(), which calls Frame("") (link).
So, since an implicit super() is added if you don't specify a call to any super constructor yourself, it would be (although very slightly so) more effective to call super("Title").
If you're in your constructor, try to delegate as much functionality as possible to the super constructor. It's possible that you can save it from doing some work.
For instance, the default super constructor might create some inner objects that will just get overwritten when you call the setter. If you pass the correct data immediately, you give it the opportunity to be more efficient.
But I think in this specific case, it does not matter much.
It is a good practice to always call the corresponding super(.....) when you extend a class.
As you never know what magic the super constructor does behind the scene.
You never need just
call super();
That's what will be there if you don't specify anything else. 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

What is Implicit constructors on Java

Is it mandatory to call base class constructor in Java?
In C++ it was optional, so I am asking this.
When I extend ArrayAdapter, I get this error: "Implicit super constructor ArrayAdapter<String>() is undefined. Must explicitly invoke another constructor"
So, what is the purpose of calling base constructor? When I create object base class constructor will call & then it comes to derived right.
The no-args constructor is called implicitly if you don't call one yourself, which is invalid if that constructor doesn't exist. The reason it is required to call a super constructor is that the superclass usually has some state it expects to be in after being constructed, which may include private variables that can't be set in a sub-class. If you don't call the constructor, it would leave the object in a probably invalid state, which can cause all kinds of problems.
It's not necessary to call the no-args constructor of super class. If you want to call a constructor with args, user super keyword like show below:
super(arg1, ...);

Categories

Resources