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.
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.
I know that all variables in any classes normally should be encapsulated, And another one could not be access but itself.
But in case of child class, I wonder that why child class can not access its variables that inherit from mother class (e.g. Mother class has variable A = "" and when you create Child class that extends from Mother class. you are not allowed to change variable A to another value (overriding) immediately in Child class).
And yes, there are another way to do this we can use constructor and super keyword to solve this problem. the question is that why they do not allow us to do it more simply way? for example just allow us to override variables that much much more easier that using constructor and super keyword. I think should have some reason to support this. Right?
It's about protecting the internal implementations of the objects involved, and providing the author of the subclass with a contract that limits what the subclass needs to know about the superclass.
It should be up to the author of a class what the scope of its instance variables are, and the way that the author controls that scope is by assigning access modifiers to them or by passing references to them through method calls.
If the language allowed the subclasses to change any variables of any superclass then there would be too much potential for the subclass to break the superclass, and no way to change the superclass implementation details without risking breakage to the subclasses.
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 have an abstract class called Policy, and two subclasses DepreciablePolicy and ExpirablePolicy
I have an array of Policy, policies[]
I want to check what subclass my object is in (if it's a Policy, DepreciablePolicy or ExpirablePolicy)
I did this by using this if statement.
if (this.policies[polNum] instanceof DepreciablePolicy){
For each type of subclass, there is a different method I have to run.
Only problem is that I'm only able to use the Policy methods, but not the subclass methods or constructors.
Is there a way I can do this?
This is inheritance done the wrong way. The whole point of polymorphism is that your code doesn't care which subclass an object is. Anything you need to do should be expressed through the Policy, which should really be an interface. Your code should interact with a Policy based on that interface, and the subclasses choose how to react.
That being said, you're probably looking for simple down-casting:
DepreciablePolicy d = (DepreciablePolicy) policy;
This is nothing better than a poor bandage on a bad design, though.
For each type of subclass, there is a different method I have to run.
Then you've misdesigned it. Define an abstract method in Policy, have all the derived classes implement it according to their own requirements. Then just call the method.
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.