The protected
keyword in Java and other object-oriented programming language always seemed pretty strange. Unlike the private
and public
it's not obvious why anyone would need it.
Now I find myself using it quite a bit. Here's the scenario.
I'm extracting methods into a super class so that I can share them among related classes. One of those methods I'm extracting is private
. What to do? I don't want to make it public
. If I leave it private
nothing will work. That's where protected
is appropriate.
All of this is made more natural in an IDE that has support for refactoring.