posted 21 years ago
This is a good example -- protected is the least understood modifier. Probably because books often say, "Protected means subclasses outside the package can access the protected member."
Sort of... the access is restricted JUST TO INHERITANCE. So a subclass can access the member by inheriting it, and that's it. The subclass doesn't get to make an instance of the parent class and then access the member that way, using the dot operator.
It's like the parent class says, "I'm marking this member protected, so that you -- my subclass outside the package -- can use it, but the only way you get to use it because you inherit it. So you can read it and write it, but you can't give anyone else access to it! It behaves as though it were private to your class when YOU use it. It's as if you inherited a private member, that only you get to use. Nobody who has a reference to you can use your reference to see it (well, unless they are another instance of your same class, but that's always true for private members). And you cannot use a reference to me -- the superclass -- and access it using the dot operator on a reference to me. If you try to do that, it will be as if it were private to ME. But again, you DO get to inherit it, so that you can use it in your own methods, etc."
cheers,
Kathy