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.
Related
I'm currently taking an intro level CS course at my university. My professor insists on using this.method() rather than super.method().
Example: I'm calling a method from a superclass, getOneIntersectingObject(), and since it's in the superclass I usually call it by using super.getOneIntersectingObject(). However, my professor wants me to call it using this.getOneIntersectingObject().
I understand that this searches the current class and executes its method() over any similarly named method() in any superclasses, but my question is this:
Why use this if you're not overriding the method? Is this just common programming etiquette?
Why use this if you're not overriding the method?
Because
It's less complicated to use this consistently rather than using this or super depending on what method you're calling.
You may add an override later; using super would require that you go back and fix all of the places you called it.
And yes, because it's the normal thing to do.
This is a CW, anyone else who wants to jump in with reasons, please don't hesitate.
You don't have to use this or super if you don't use an override. If you have an override, this.method() calls the method of the child class (derived class) and super.method() calls the method of parent class. super.method() is like saying, "i want parent class's method() to execute, not the child's".
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.
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.
I have a class U and in the constuctor of the class I call overridable method which is public.
NetBeans warns me: Overridable method call in constructor
However, I don't override the method in my project, since class U has no subclasses..
Is it OK to leave it like this? ....calling overridable method in the constructor in this case?
It is not an error. You can ignore it.
If you want to make the compiler happy, make either method, or the whole class final.
As already mentioned you can 'ignore' the warning.
However you do so at your own risk as bugs can creep in at a later date. The reason for the warning is that the compiler cannot prove that a reference to 'this' will not escape the constructor. Which can lead to bugs, as the object being created has not been fully constructed yet and thus the object can be in an invalid state.
It is a warning, not an error, so you can just leave it like this. If you would publish this code however, someone could extend your class U, override the method and get into a lot of trouble.
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.