Maintain the visibility of an abstract method in a subclass

I have the 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 subclass of the 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 who extends an abstract class and implements the executeAction() method does not allow the visibility of executeAction() change?

For now, a developer can simply change the visibility of the method to "public", create a subclass object, and call executeExtion() . The visibility modifier can be changed, and the abstract method is still accepted as "implemented."

Thus, the โ€œnormalโ€ sequence of calls and checks that are performed in the invoke() abstract class method can be bypassed. Is there any way to check if invoke() method was called?

+4
source share
3 answers

No, there really is no way to limit this. Are you worried about malicious developers or clueless colleagues? if the latter, then you just need to establish coding conventions, such as "do not increase the visibility of methods" and put some javadoc on an abstract method indicating proper use. if the first, then you probably have to develop your code in different ways (perhaps using a strategy template).

+3
source

Is it possible that a developer who extends an abstract class and implements the executeAction () method does not allow visibilty changes for executeAction ()?

No, It is Immpossible.

Chapter 8.4.8.3. Requirements for overriding and hiding the Java language specification indicate:

The access modifier (ยง6.6) of the override or hide method must provide at least the same access as the overridden or hidden method, as follows: ...

Thus, it is always possible for an overriding method to provide access more than an overridden method in the parent class.

See also java access modifiers and override methods .

+4
source

It is allowed to change the modifier to public, since it does not violate the Liskov replacement principle .

So, the "normal" sequence of calls and checks that are performed in the invoke () abstract class method can be bypassed. Is there any way to check if invoke () method is called?

If you pass someone a reference to AbstractCreateActionHandler , then the caller will not be able to see the executeAction method, because it is not public in the AbstractCreateActionHandler class. Thus, the caller will not be able to bypass the execution sequence if you pass a reference to the base class for the caller. If you pass a reference to the Concrete class, the sequence may be broken.

0
source

All Articles