Class A derives from abstract class B and implements the abstract method foo. Now in method foo I want to do something which depends on a member variable of class A, mType. However this results in a bug because foo is called in the constructor from abstract class B, hence mType isn't initialised yet.
It is not possible to initialise mType before the super() so I don't know a good and clean way to go about this. Of course I can make mType a member of B but I think this isn't a good way to go because mType doesn't have anything to do with this class, in this example this is perhaps not clear, but of course I've rewritten the practical situation to a short simple one in order to explain the problem.
What is a good way to tackle this problem?
public abstract class B {
public B() {
foo(); // A::mType IS NOT INITIALISED!!
}
protected abstract void foo();
}
private class A extends B {
public enum Type { TYPE1, TYPE2 };
public A(Type aType) {
super();
mType = aType;
}
@Override
protected void foo() {
if (mType == Type.TYPE1) {
// ..
} else {
// ...
}
}
}