Reputation:
I'm sure this will turn out to be a stupid question, but can someone tell me, in Java (although I'm sure this must have some relevance to other languages), what a superclass constructor is actually doing?
Say I have a parent class with a constructor that sets a variable (that isn't necessarily a static one) from the parameter given to the constructor, and then I call the superclass constructor in the constructor of a child class. If I haven't ever instantiated the parent class (although it's the same if I have), what is that superclass constructor actually referring to (the class itself or a non-existent object of it?), and where would that variable that it set actually have been set? Surely it has to be something else if a superclass constructor is able to alter variables that aren't static? If it alters a non-static variable but that class is never instantiated, what happens to the value of that variable? How does one access it and so on?
Sorry if this is hard to understand, but hopefully someone can explain this? And again, I'm sure this will turn out to be a stupid question.
Thanks
Upvotes: 2
Views: 129
Reputation: 40036
If I haven't ever instantiated the parent class (although it's the same if I have), what is that superclass constructor actually referring to (the class itself or a non-existent object of it?)
This will not happen. (I believe what you said is you haven't invoked any constructor of parent class, right?)
Constructor of parent will always be invoked, although usually not explicitly. Whenever you have a child class, and in its constructor you haven't explicitly invoked parent class' constructor (by using super(...)
, compiler will silently helped you to invoke the no-arg constructor of parent class.
i.e.
class Parent {
int parentVar = 0;
public Parent() {
parentVar = 1;
}
}
class Child extends Parent {
int childVar;
public Child() {
childVar = 10;
}
}
Although you haven't invoked Parent
's constrcutor in Child()
, compiler is implicitly helping you to add a line of super()
at the first line in Child
constructor, hence it is actually doing:
public Child() {
super();
childVar = 10;
}
So you will see parentVar
set to 1
when you instantiate a Child
Upvotes: 0
Reputation: 262494
While constructors run, an instance of the object is already allocated, but it has not been properly initialized (that is the job of the constructor after all).
As a result, within the constructor, you do get access to the current instance (this
), but before the constructor completes, handle with care.
This can become a problem for example, if a constructor calls into non-final methods (that may later be overridden).
Similarly, super
will refer to the superclass instance (and Java makes sure that the first thing you do in your constructor is to call one of the super
constructors, so that as soon as your own constructor code starts, the inherited fields are all initialized).
If your parent class requires parameters to be passed to the constructor, then all subclass constructors have to provide that parameter as the first line of code. The compiler will not accept a subclass that fails to do so.
Upvotes: 2