Howdy,
I'm not sure what they were trying to explain, but I can tell you what the problem is...
(and this is perhaps the most commonly misunderstood part of
Java access, so pretty much everybody has trouble with this!)
It centers around what the 'protected' modifier really means. You'll often see it in charts and tables like this:
default: accessible only to classes in the same package.
protected: accessible only to classes in the same package or subclasses, even when the subclasses are outside the package.
But that just doesn't explain it!!
Because for 'protected', you really do NOT have full access to a protected member of your superclass, if you're a subclass and outside the package of the superclass.
'protected' means "Access through Inheritance ONLY". In other words, if a member is marked 'protected', subclasses outside the package can access that member ONLY by inheriting it. The subclass "has" the member, but they can't ACCESS the member using an instance of the superclass.
So, class B can't make an instance of A and then access the protected 'i' variable.
But... class B can access this.i. B can get his *own* inherited 'i' variable, but he cannot ask for the 'i' variable of an instance of A, the superclass.
Bottom line: You can't use a superclass reference to get to a protected member of your superclass if you are not in the same package as the superclass. However, you CAN use your own 'this' reference to access the protected member, because you definitely inherited it.
One more note while we're here...
With protected, what happens to the subclass outside the package, when IT has subclasses? Can those subclasses still inherit the protected 'i'? Yes. Once a member is marked 'protected', that member is inherited all the way down regardless of where the other subclasses live (in other words, it makes no difference which packages the subclasses are in). BUT... once you have a subclass outside the package, the *other* classes in the subclass-outside-the-package's package cannot access the protected member. For example, ANY class in A's package can access 'i' -- it acts default within the class in which the member was declared. But within B, nobody in B's package can access 'i', so from that perspective the protected member acts as though it were marked private! Except... it isn't *completely* private, because subclasses of B will still *inherit* the member.
So, 'protected' is a very strange modifier, because it means "access through inheritance only", except for other classes defined within the same package as the class that declared the protected member (in which case, the protected member is just like 'default' access).
Whew!
I probably just made things more confusing
cheers,
Kathy