tags:

views:

183

answers:

5

I need this because the constructor in the superclass is calling a method which is overridden in the subclass. The method returns a value which is passed to the subclass' constructor. But the superclass constructor must be called before the subclass constructor, so I have no chance to save the value passed in.

+4  A: 

Calling an overridden method from the superclass constructor is simply not going to work - don't do it. The superclass constructor must always finish before that of the subclass. While the superclass constructor is executing, the object in question is a (half initialized) instance of the superclass, not the subclass! So if you try to call any overridden function from the constructor, the subclass fields it may depend on are not yet initialized (just as you have observed). This is a fundamental fact of class design, and there is no workaround.

As explained in Effective Java 2nd. Ed. (Chapter 4, Item 17):

There are [...] restrictions that a class must obey to allow inheritance. Constructors must not invoke overridable methods, directly or indirectly. If you violate this rule, program failure will result. The superclass constructor runs before the subclass constructor, so the overriding method in the subclass will get invoked before the subclass constructor has run. If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected.

If you can change the superclass implementation, try moving the call to the virtual function out of the constructor. One way to achieve this is using a Factory Method:

class Super {
    public void init() { ... }
}

class Subclass extends Super {
    private Subclass() { super(); ... }
    public void init() { super.init(); ... }
    public static Subclass createInstance() {
        Subclass instance = new Subclass();
        instance.init();
        return instance;
    }
}

Note that the constructor of Subclass is private to ensure that it can only be instantiated via createInstance(), thus instances are always initialized properly. OTOH this also prevents further subclassing. However, subclassing a concrete class is not recommended anyway - a class meant to be subclassed should be abstract (with a protected constructor in this case). And of course, any further subclasses must also have non-public constructors and static factory methods which diligently call init()...

Péter Török
Even init methods like this are bad form. In my experience situations like this raise the question: should you really be using inheritence here? The answer is probably no.
Andreas
@Andreas IMO your question can not be answered without knowing the larger context of this design - which is completely omitted by the OP.
Péter Török
I think the factory method suggested by you could be polished if we place it in the Super class. That way we would pass a Class type as an argument for createInstance and we would instantiate the passed type by calling newInstance(). After that, we call init(). Also, we could return a subclass of Super by declaring <T ? extends Super> T as return type. This way you don't have to write a createInstance() method for every subclass. I haven't tried it yet, but I think it would work.
kahoon
@kahoon - seems doable, although more difficult to understand and use. In exchange it eliminates duplication. Everything has a price :-) For a big enough class hierarchy, it may pay back its price.
Péter Török
+2  A: 

Your question is summarized by the following snippet:

abstract class Base {
    Base() {
        dontdoit();
    }
    abstract void dontdoit();
}
public class Child extends Base {
    final int x;
    Child(int x) {
        this.x = x;
    }
    @Override void dontdoit() {
        System.out.println(x);
    }

    public static void main(String args[]) {
        new Child(42); // prints "0" instead of "42"!!!
    }
}

This is a quote from Josh Bloch and Neal Gafter's Java Puzzlers: Traps, Pitfalls, and Corner Cases:

The problem arises whenever a constructor calls a method that has been overridden in its subclass. A method invoked in this way always runs before the instance has been initialized, when its declared fields still have their default values. To avoid this problem, never call overridable methods from constructors, either directly or indirectly [EJ Item 15]. This prohibition extends to instance initializers and the bodies of the pseudoconstructors readObject and clone. (These methods are called pseudoconstructors because they create objects without invoking a constructor.)

In short: DON'T DO IT!

Moreover, the book proposes a possible solution (slightly edited for generality):

You can fix the problem by initializing the field lazily, when it is first used, rather than eagerly, when the instance is created.

polygenelubricants
A: 

I know the problem. It is not possible. If the super class is not your own and you have no other options then you can do some bad hacks. But it is not recommended.

Solution 1:

Use a static factory with a synchronized. This can look like (redudced):

private static String savedValue;

public static Abc create(String value){
  synchronized(Abc.class){
    savedValue = value;
    Abc abc = new Abc();
    savedValue = null;
    return abc;
  }

private Abc(){
}

private value;

String getValue(){
  if( value == null ){
    value = savedValue;
  }
  return value;
}

Solution 2:

Make a differentiation in your method if the own constructor was already run with a flag. Then you need to duplicate some stuff of the super constructor with the real value.

Horcrux7
A: 

If this is a problem you have a fundamental problem in your class hierarchy; superclasses cannot know about any specific subclass because there might be several, they might even be loaded dynamically at runtime. That is why any super() calls in a constructor must be on the first line.

As a general rule, don't call any public methods in a constructor.

Adriaan Koster
The superclass doesn't know about the subclass, in fact the whole purpose of using an overridden method is to eliminate the need to know about the subclass.
kahoon
Thanks, you are right! So correctly stated the problem is calling a public method before the instance has been fully initialized.
Adriaan Koster
A: 

You didn't mention whether you can modify the superclass's constructor at all. If you can, then this is doable. As other have said, this is not advisable, but here's a contrived example that does what you're looking for--the superclass calls an overridden method which uses an argument that was passed to the subclass's constructor:

public abstract class Base {
        public Base(int value) {
                init(value);
                System.out.println(getValue());
        }

        public abstract void init(int value);
        public abstract int getValue();
}

public class Derived extends Base {
        private int value;

        public Derived(int value) {
                super(value);
        }
        public void init(int value) {
                this.value = value;
        }
        public int getValue() {
                return value;
        }
        public static void main(String[] argv) {
                new Derived(25);
        }
}

> java Derived
25

Angus