It is possible to do this, but not by overriding initialize
. In the BaseTest
class, mark initialize
as final
so it cannot be overridden. In initialize
, call another method to do any subclass initialization.
public final void initialize() {
mIsInitialized = true;
initializeFurther();
}
You said no abstract
methods. But, initializeFurther
can just be made empty.
protected void initializeFurther() {}
Then, the subclass Test
just needs to override initializeFurther
.
public class Test extends BaseTest {
@Override
protected void initializeFurther() {
// Initialize Test here.
}
}
The base class's initialize
method is guaranteed to set mIsInitialized
to true
, and Test
can implement initializeFurther
however it wants, without calling super.initialize()
. Test
can't stop the superclass BaseTest
from doing its initialization.
When initialize
is called on any BaseTest
instance, the initialize
logic will always run.