I have an abstract class Entity
. Every class extending Entity
will need some default and some customizable setup:
public abstract class Entity {
protected Entity() {
// ... default setup
customSetup();
}
protected abstract void customSetup();
// ...
}
My extending class MyEntity
takes a parameter in the constructor, that will be used in customSetup()
:
public class MyEntity extends Entity {
private Data data;
public MyEntity(Data d) {
super(); // in here customSetup() is called!
data = d;
}
@Override
protected void customSetup() {
codeDependingOn(data); // will throw NPE: data==null yet!
}
}
As comments state, this code won't work.
I could just throw away customSetup()
and put all the custom code after super()
, but having that abstract method makes clearer what you're supposed to put there.
I feel like I'm violating some rule of OOP design. What's the correct way to do what I want?