views:

106

answers:

4

Please refer to the Java code below:

class Base{
     Base(){
         System.out.println("Base Constructor");
         method();
     }
     void method(){}    
}

class Derived extends Base{
    int var = 2;
    Derived(){
         System.out.println("Derived Constructor");  
    }

     @Override
     void method(){
        System.out.println("var = "+var);
     }
 }

class Test2{
    public static void main(String[] args) {
        Derived b = new Derived();
    }
}

The output seen is:

Base Constructor
var = 0
Derived Constructor

I think var = 0 occurs because Derived object is half initialized; similar to what Jon Skeet says here

My questions are:

Why does the overridden method get called if the Derived class object isn't created yet?

At what point in time is var assigned value 0?

Are there any use cases where such behavior is desired?

+5  A: 
  • The Derived object has been created - it's just that the constructor hasn't been run yet. The type of an object never changes in Java after the instant it is created, which happens before all constructors run.

  • var is assigned the default value of 0 as part of the process of creating an object, before constructors are run. Basically, the type reference gets set and the rest of the memory representing the object gets wiped to zero (conceptually, anyway - it may already have been wiped to zero before, as part of garbage collection)

  • This behaviour at least leads to consistency, but it can be a pain. In terms of consistency, suppose you had a read-only subclass of a mutable base class. The base class may have an isMutable() property which was effectively defaulted to true - but the subclass overrode it to always return false. It would be odd for the object to be mutable before the subclass constructor ran, but immutable afterwards. On the other hand, it's definitely strange in situations where you end up running code in a class before the constructor for that class has run :(

A few guidelines:

  • Try not to do much work in a constructor. One way of avoiding this is to do work in a static method, and then make the final part of the static method a constructor call which simply sets fields. Of course, this means you won't get the benefits of polymorphism while you're doing the work - but doing so in a constructor call would be dangerous anyway.

  • Try very hard to avoid calls to non-final methods during a constructor - it's very likely to cause confusion. Document any method calls you really have to make very clearly, so that anyone overriding them knows that they will be called before initialization has finished.

  • If you have to call a method during construction, it's usually not then appropriate to call it afterwards. If that's the case, document it and attempt to indicate it in the name.

  • Try not to overuse inheritance in the first place - this is only going to become an issue when you've got a subclass deriving from a superclass other than Object :) Designing for inheritance is tricky.

Jon Skeet
In Swing it is common to run `create` method is derived classes before the constructor [body] has run. Swing is *definitely* strange.
Tom Hawtin - tackline
@Tom, which means that the `create` method may never depend on object state of the derived class.
rsp
@rsp Yup. It will usually depend upon the type of the derived class. It can actually depend on state introduced from an "outer `this`", or through a hack such as `ThreadLocal`.
Tom Hawtin - tackline
@Jon Skeet: Thanks for the cogent explanation!
Abhi
+2  A: 

There are some properties of the Java language specification that should be noted in order to explain this behavior:

  • A superclass' constructor is always implicitely/explicitely called before a subclass' constructor.
  • A method call from a constructor is just like any other method call; if the method is a non-final, then the call is a virtual call, meaning that the method implementation to invoke is the one associated with the runtime type of the object.
  • Prior to a constructor execution, all data members are automatically initialized with default values (0 for numeric primitives, null for objects, false for boolean).

The sequence of events is as follows:

  1. An instance of the subclass is created
  2. All data members are initialized with default values
  3. The constructor being invoked immediately delegates control to the relevant superclass' constructor.
  4. The super constructor initializes some/all of its own data members, and then calls a virtual method.
  5. The method is overriden by the subclass, so the subclass implementation is invoked.
  6. The method tries to use the subclass' data members, assuming they are already initialized, but this is not the case - the call stack hasn't returned to the subclass' constructor yet.

In short, whenever a constructor of a superclass invokes a non-final method, we have the potential risk of entering into this trap, therefore doing it is not recommended. Note that there is no elegant solution if you insist on this pattern. Here are 2 complex and creative ones, both requiring thread synchronization(!):

http://www.javaspecialists.eu/archive/Issue086.html

http://www.javaspecialists.eu/archive/Issue086b.html

Eyal Schneider
That's not really the reason, as the construction order is the same in C++. I can't see how it could ever be any different actually. Yet this issue doesn't arise in C++. The true reason is that the object is of the target type throughout construction, which *isn't* the case in C++.
EJP
@EJP: I am not comparing with C++. All i'm saing is that two facts cause this behavior: 1) Super's ctor is always called first 2) A call to a method from the ctor is not different than any call, therefore it is a virtual call unless the method is final (private/declared as final). See the links I provided for more details on this design problem.
Eyal Schneider
I agree with the *two* facts, now you've stated them both ;-)
EJP
@EJP: made my explanation better, thanks
Eyal Schneider
@Eyal: Well explained. Thanks!
Abhi
+2  A: 

Why does the overridden method get called if the Derived class object isn't created yet?

Derived class constructor implicitly calls the Base class constructor as the first statement. Base class constructor calls method() which invokes the overridden implemention in the Derived class because that is the class whose object is being created. method() in Derived class sees var as 0 at that point.

At what point in time is var assigned value 0?

var is assigned the default value for int type i.e. 0 before the contructor of Derived class is invoked. It gets assigned the value of 2 after the implicit superclass contructor call has finished and before the statements in Derived class's constructor start executing.

Are there any use cases where such behavior is desired?

It is generally a bad idea to use non-final non-private methods in the constructors/initializers of a non-final class. The reasons are evident in your code. If the object that is being created is a subclass instance, the methods may give unexpected results.

Samit G.
A: 

Note that this is different from C++, where the type does change while the object is being constructed, so that calling a virtual method from the base class constructors doesn't call the derived class's override. The same thing happens in reverse during destruction. So this can be a small trap for C++ programmers coming to Java.

EJP
@EJP: Yeah, always good to know that the type can change in C++ and it cannot in Java.
Abhi