When to Use protected

| No Comments

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.

About this Entry

This page contains a single entry by Christian published on July 9, 2004 2:55 PM.

Privacy and Context was the previous entry in this blog.

Estimates: a Useful Tip is the next entry in this blog.

Find recent content on the main index or look in the archive to find all content.