Keep visibility of abstract method in subclass
abstract-class, java, visibility
Solution
No, there's not really a way to restrict that. are you worried about malicious developers or clueless coworkers? if the latter then you just need to establish coding conventions like "don't increase the visibility of methods" and put some javadoc on the abstract method indicating proper usage. if the former, then you probably need to design your code differently (possibly using the strategy pattern).
Problem
I have following abstract class: ``` public abstract class AbstractCreateActionHandler { protected IWorkItem mCurrentWI; public AbstractCreateActionHandler(IWorkItem wi) { this.mCurrentWI = wi; } public final void invoke() { try { if (checkForLockingFile()) { this.executeAction(); Configuration.deleteInstance(); } } catch (IOException e) { Configuration.deleteInstance(); e.printStackTrace(); } } protected abstract void executeAction(); private boolean checkForLockingFile() throws IOException { String path = Configuration.getInstance().getProperty("path"); File lock = new File(path + "lock_"+mCurrentWI.getId()+"__.tmp"); if(!lock.exists()) { lock.createNewFile(); return true; } return false; } } ``` A sub class extends the abstract class: ``` public class MyAction extends AbstractCreateActionHandler { public MyAction(IWorkItem wi) { super(wi); } @Override protected void executeAction() { // Implementation } // ALSO POSSIBLE... /* @Override public void executeAction() { // Implementation }*/ } ``` Question: Is it possible that a developer which extends the abstract class and implements `executeAction()` method is not allowed the change the visibilty of `executeAction()`? At the moment a developer can simply change the visibilty of the method to "public", create an object of the subclass and invoke `executeExtion()`. The visibility modifier can be changed and the abstract method is still accepted as "implemented". So the "normal" calling sequence and checks which are executed in abstract class method `invoke()` can be bypassed. Is there a way to check if the `invoke()` method was called?