Revisiting the protected access modifier
Recall that a protected
member is accessible from within its own package and any subclasses outside of the package: protected = package + children. On the face of it, this seems very straightforward. However, some nuances lead to confusion. The subclasses that access the protected
member (via inheritance), can only do so in a very specific way. A subclass from outside the package cannot use a superclass reference to access the protected
member! In addition, an unrelated class from outside the package cannot use a reference to the subclass outside the package either to access the protected
member. In effect, once the subclass that’s outside the package inherits the protected
member, that member becomes private
to the subclass (and subclasses of the subclass). This is quite tricky and definitely needs an example.
The UML diagram
Figure 9.11 shows the UML diagram for this example:
Figure 9.11 – UML...