I am going through a past Java exam and I have a question that I'm stuck on.
The question is: "Any constructor either explicitly or automatically calls the constructor of its parent class, which calls its parent, and so on up the class hierarchy. What is the name of this process?
Thanks for your answers!
It's called "constructor chaining"
#Edit: adding source: Oracle's documentation. Courtsey of Oli Charlesworth.
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.
Related
In java.util.LinkedList class, there are 2 constructors defined, default and parameterized that accepts a collection. When I looked at the implementation, I see that the parameterized constructor has this(), basically is calling the default constructor.
But the default constructor is empty. So I was just curious if this is a best practice to call default constructor even if its empty or is there any reasoning behind this. What happens if we don't call default constructor.
GrepCode LinkedList implementation
If the default constructor is extended at any time in the future, the other constructor does not have to be changed.
It tells us, that the extended constructor is basically a default constructor, but with some added special functionality (for convenience).
It simply means any form on initialization of LinkedList must perform default initialization steps first.
It might seem weird to you as there are no default initialization steps as of now (but might come up later).
It's not really a recognized good practice. Maybe the default constructor did something in previous versions. Maybe the developer thought it would be a good idea, so that if something additional is done in the default constructor, it will also be done in the other one. If you look at ArrayList, you'll see that a different choice was made.
What happens if we don't call default constructor.
The default constructor is just not called. The superclass no-arg constructor is called implicitely.
The parametrized version should create a list the same way as not parametrized, and call a method to add elements additionally. The fact that default constructor is empty is implementation specific.
/**
* Constructs an empty list.
*/
public LinkedList() {
}
The fact that it's intended to construct it this way remains.
This question already has answers here:
Why should I call super() in Java?
(5 answers)
Closed 8 years ago.
What's the point in invoking super()-- the constructor of the super class from within a constructor when the super constructor is doing nothing?
Java APIs are doing it-- saw ArrayList for one,
invoking AbstractList's constructor in its own.
protected AbstractList() { }
is the only constructor of AbstractList.
without the super() call, it'll run anyway-- and with have the same effect.
TIA.
With the default superclass constructor, it's simply a matter of style. If you have no call to another constructor at the start of your constructor (either super(...) or this(...)), then the compiler automatically inserts a call to super(). (The only class that's exempt from this rule is java.lang.Object.)
I don't think there is a point. I've seen some IDEs auto-generate code that uses an explicit call to super() when it's not needed. I wish they wouldn't. It's a coding style I don't care for.
Your subclass will call superclass even if you will not call super() in constructor of subclass.
First of all, the call to super constructor is always made, unless you call another constructor with this(). If you don't type super() at beginning of your constructor, the compiler will type it there for you. (I mean it will compile the file as if you would type it there)
Moreover, if you ask why is the super constructor called at all if it does nothing, it is because (one of the reasons) the super class can change and the constructor can do something in the newer version, and without recompiling the subclass, you already call the super constructor.
I don't think there's any practical reason to use it, per se, because you are correct - it gets called automatically if no super() constructor call is made explicitly.
However, I'll play devil's advocate here and say that I like calls to super() simply because it's more clear with respect to how initialization is actually happening. I've found this to be especially true when you have newer Java developers who might be taking over that particular piece of the code after you get done with it - inexperienced developers down the road will be less prone to making incorrect assumptions about how things actually get initialized, and that's usually worth the minor cost of being overly specific.
If I go to the Javadoc page for the RandomGenerator class I see a summary of the class constructor and methods. It says public class RandomGenerator extends Random.
How can I find out more information about how RandomGenator class invokes the constructor of Random class? I don't see any references in RandomGenerator class to it's superclass constructor.
Most likely you have to contact authors. For some reason they decided not to include this information into javadoc (public contract).
You may also look into class sources or use java decompiler if you don't have sources.
In case class constructor doesn't call super() directly that means that superclass has no-argument constructor which call is the first line of your class constructor always, and you do not need to call it explicitly.
P. S. My answer has two different approaches. I apologize for that, I just not quite sure whether you have sources for analysis.
The short answer is, you can't from the javadocs directly.
You can look at the parent class, find that it has 2 constructors, guess which one is being called based on the parameters the child class constructor exposes.
And ofcourse the most conclusive answer can be given by looking at the source code if available or decompiling the class if it's not.
From the documentation:
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.
Therefore, they just decided not to actually write the call to the super constructor and use the default one.
I wanted to know if there is possibility to generate constructors for all subclasses in IntelliJ.
I mean if i have many subclasses and somewhere in development stage I wanted to add new constructor, with a flag, for example. Is there a way to generate this constructor in all subclasses too?
I wanted to know if there is possibility to generate constructors for all
subclasses in IntelliJ?
The answer would be no and one of the reason would because it is design specific.
When you create a subclass and define a non default constructor in it you have the liberty to call any one of the overloaded superclass constructor from it. Which one would be design choice as initialization of superclass variables may depend on it. Never the less call to one of the overloaded super class constructor must be 1st statement of subclass constructor. If you do not provide any, call to default constructor of super class is inserted by default by the JVM. However in that case if your super class does not have default constructor(you have explicitly provided some parameterized constructor) then compiler error will occur.
When I run static analysis on the following code:
public ExtractDBScripts(String resBundleName)
{
super();
m_mainBundle = ResourceBundle.getBundle(resBundleName);
}
I get the following error:
"JAVA 0058 Constructor 'ExtractDBScripts' calls super()".
What is wrong with calling super() from a constructor?
Probably just that it's completely unnecessary--that is java's default behavior (it will call super for you). You want to use the explicit call to super() if you need to pass a parameter to a non-default constructor.
A static analysis tool will often point out code that does absolutely nothing or is unnecessary to help you reduce clutter. It will also point out a=a; there is nothing wrong with saying a=a; but it's not actually doing anything.
Absolutely nothing wrong with it - although it is implicitly there as the first line of a constructor if you don't declare it yourself (i.e. there's no need to declare it)
I presume the tool you are using is objecting to that line of code because it is not required - if you remove it the compiler will automatically insert it.
See the "Subclass Constructors" section here.
There is nothing wrong with calling super() within a constructor so long as it is the first line in your constructor, so long as you are actually extending a class that has a non private constructor.
If the class you are extending has only one constructor, and it's private then you can't call super().
I would suggest calling super() in your code on purpose, so that it is obvious to other coders what you want to do. It is part of the programming practices where I work and seems to make sense. There may be a way for you to set up the static analysis tool to ignore calls to super() also depending on the software. IntelliJ has annotations for ignoring specific things when it does static analysis.