how to restrict protected method access to only subclasses

It is not possible. You have a choice between protected modifier (subclasses + classes in the same package) and default modifier (classes in the same package). There is no third option.

Also you cannot easily enforce that at runtime as it is not simple to find the class name and package of the calling code. See: How do I find the caller of a method using stacktrace or reflection?

One option is using . It can even work at compile time to reject code trying to access protected method from the same package while not being a subclass (with a help of declare warning and declare error directives). You probably want to include some compile-time annotation like @SubclassesOnly. See: Compile-time architecture enforcement revisited: AspectJ, Maven and Eclipse and Compile-time checks with AspectJ.

Leave a Comment