The fact that the intended behavior, without setting the final method, means that it can be override n, so you should always consider that someone can do this. A call to this method is never guaranteed for a method at this level.
You can solve this problem elegantly using the ( protected ) final method:
class MainClass { protected final void innerDoIt () {
And then:
class SubClass extends MainClass { @Override public doIt() { super.doIt(); ... } }
final ensures that the method cannot be overridden. So, at that moment you have a contract (guarantee) that the innerDoIt method innerDoIt indeed the innerDoIt method, which, in your opinion, is.
Thus, in the case you do not want the caller to stop , just hedge it into another final method. By making it protected , this method can also be called using SubClass .
Willem van onsem
source share