The not so protected Java protected member

By | April 9, 2007

This week I found out something new. At collage I was always tought that a in programming you need to protect a class members. One way of doing this is using the protected level. According to many programming languages, like C++ and pascal, this means only inheriting classes can access and modify the value.

To my suprise and somewhat dislike Java does not know this type of protection level. In Java protected means the same as the default and extends it so that all inheriting classes can access the members. Unfortunatelly the default access level means that all classes in the same package (namespace in C++) can also access it.

I never expected this which introduces flaws in design. After all I don’t want my personal protected members to be accessed anywhere within the package. Only the inheriting classes.

Leave a Reply