Work around protected method definitions

Sometimes you want to call a protected method but you class is not a subclass and neither in the same package.

The obvious solution that comes to mind is to increase the visibility of the called method. This however may not be possible since you don’t own the class (e.g. part of a third part library) or even more important: Bad Design.
If you cannot/will not change the interface you are left with the possibility to wrap the call. To do this you simply define a private inner class that extends the class containing the method. From this inner class you can call the method. Now define a public method that wraps this call. Arguments and return values are passed through. This public method can now be called from your own code.

Ein Gedanke zu „Work around protected method definitions“

Schreibe einen Kommentar