Object Oriented Programming: (implicit) acknowledgement of the missing access modifier

❝The missing access modifier is resolved using a static analysis and @ForOverride-annotation❞

In 2016, I noticed a curiosity about Java’s access modifiers: there is no mechanism to express that a method may only be overridden or called from within the child classes. This may be an important feature, as there are designs where you would want to allow overriding – or in particular – extending in child classes, without the risk of it being used from within the package in unexpected ways or at unexpected times.

Google Error-Prone is a static analysis library that performs additional, more complex static analysis during compile-time. This allows for additional checks for various use cases. One of these use cases is exactly this pattern. The annotation @ForOverride allows the developer to annotate specific methods such that analysis will fail if the method is used inappropriately, that is called from within the package outside the class itself. This covers the missing part of Java access modifiers.

This post is part of the Object Oriented Programming series.
Other posts in this series: