We always tacitly assumed that protected modifier in java permits member access from a class the member belongs to, or from an instance of class's descendant. Very like the C++ defines it, in fact.
protected
In other words no external client of an instance can directly access a protected member of that instance or class the instance belongs to.
It would be very interesting to know how many people live with such a naivete, really!
Well, that's what java states:
The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
If one'll think, just a little, she'll see that this gorgeous definition is so different from C++'s and so meaningless that they would better dropped this modifier altogether.
The hole is so huge that I can easily build an example showing how to modify protected member of some other class in a perfectly valid way. Consider:
MyClass.java
package com.mypackage; import javax.faces.component.Hack; import javax.faces.component.UIComponentBase; import javax.faces.event.FacesListener; public class MyClass { public void addFacesListener( UIComponentBase component, FacesListener listener) { Hack.addFacesListener(component, listener); } ... }
Hack.java
package javax.faces.component; import javax.faces.event.FacesListener; public class Hack { public static void addFacesListener( UIComponentBase component, FacesListener listener) { component.addFacesListener(listener); } }
An example is about to how one adds custom listener to an arbitrary jsf component. Notice that this is not assumed by design, as a method addFacesListener() is protected. But see how easy one can hack this dummy "protected" notion.
Update: for a proper implementation of protected please read Manifest file, a part about package sealing.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u