Javadoc identifying superclass constructor - java

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.

Related

invoking super() when it is doing nothing [duplicate]

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.

Need Clarity on Java constructors

Okay am just beginning to learn Java and I have a doubt regarding constructors.
isn't constructor just another method with same name as class?? If so Why do we need it? Cant we just use methods?
Constructors have no return type.
They are used for instantiating a class.
They can be called from subclass using Super keyword.
Their name must be same as it's classname
Constructors are called on object creation. You can initialize stuff in other methods of course but then you'd have to call them explicitly.
You don't need to write a constructor. But if you want to do things to ready your object, you put in in your constructor.

Override constructor in all subclasses IntelliJ

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.

Past Java exam about constructors

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.

Why do I need to define custom constructor?

If I have a class, let's say, extended from DialogFragment and define custom constructor for it, why should I define a default one? If I wouldn't I get the error message if runtime change occurs.
I suspect the problem is that in Java, the compiler creates a parameterless constructor for you unless you specify one yourself. If something within Android requires a parameterless constructor, then either you need to not declare any constructors yourself or you need to explicitly declare a parameterless one.
From section 8.8.9 of the Java Language Spec:
If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:
If the class being declared is the primordial class Object, then the default constructor has an empty body.
Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments.
Does that make things clear? I don't know enough about Android to know why you need a parameterless constructor, but presumably it's so that instances can be created via reflection without specifying any arguments for constructor parameters.
Constructors may contain code that is run when the object is created. It is sort of like setup code that you want done so the object is ready for what it's supposed to do.

Categories

Resources