2

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

  • There is a hierarchy of initialization during instance construction; every class constructor invokes its' super constructor up the chain to `java.lang.Object` (the root of the Java Object hierarchy). – Elliott Frisch Apr 05 '16 at 02:02

2 Answers2

2

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.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • So, every constructor in Java (except for the constructor of `java.lang.Object`) implicitly instantiates at least two object instances, because every class extends `Object`? – jaco0646 Apr 05 '16 at 02:11
  • Not really. There is just one instance (because it is one object). But it will contain all fields from all superclasses (and a constructor for each superclass will be run). – Thilo Apr 05 '16 at 02:22
  • 2
    `super` and `this` are the same instance. The keyword just tells the compiler/runtime where in the hierarchy to start doing the method lookup. – Thilo Apr 05 '16 at 02:25
  • Thanks for the answer, think I get it now. So the superclass constructor just refers to anything that the child class has inherited rather than implemented itself. Anything set by a superclass constructor will be accessible from the child object via getters/setters or public variables. Probably was a stupid question after all! –  Apr 05 '16 at 11:44
0

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

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131