Basically, what I want to do is force the subclass to call an abstract superclass method (implemented in the subclass), so I don’t have to explicitly write it each time I create a new subclass.
I wrote it in the superclass’ constructor once, because I want it to force it for every implementation.
public abstract class SupahClass {
public SupahClass() {
doStuff(); // It IS executed when the subclass constructor is called
init(); // NOT executed, even though it's implemented
}
private void doStuff() { ... }
protected abstract void init();
}
public class SomeSubClass extends SupahClass {
// The problem lies HERE: this is executed AFTER init() ... so it gets NULL again
private TextBox myTextBox = null;
public SomeSubClass() {
super(); // invokes the super constructor, so init() should be called
// I could call init(); here EACH time i create a new subclass... but no :)
}
@Override
public void init() {
this.myTextBox = new TextBox(); // Executed BEFORE its declared as null above
}
}
Of course, the superclass can’t really call it since its an abstract (so undefined) method, but its an ABSTRACT class so it cannot be instanciated, it must delegate the task to its subclasses, so why cant they call the abstract but now implemented method?
EDIT
See the subclass property myTextBox and the init() implementation
Which approach do you think I should do? Remove the = null in the property declaration (duhhh)
or remove the init() in the superclass and explicitly calling it in the subclass constructor (this is what I wanted to avoid, since I will have to write it 100% of the time..)
You can fix this by changing your declaration to:
The assignment to null serves no useful purpose. If there was no superclass, it would do nothing, because fields are initialised to null anyway. Since there is a superclass, it acts as a gunshot to the foot. So, get rid of it.