P
P.Hill
Pat said:"why does java allow a superclass to access protected overridden methods on
an instance of a subclass,
even when the subclass is in a diffrent package?".
I hadn't been following this thread closely, because I kept thinking the
behavior was what I expected, but your restatement seems weird. Why
would you ask this question? What is the problem you preceive?
If there is a superclass in the animal package called Animal with a method
called walk() and over in the quadraped package you have implementations
of Animal like Bear.walk() and Horse.walk() I would hope you
would not expect there to be any rational reason that a method in
the animal package couldn't call walk() or any other protected method
on any class which implements the method.
Yes members of the same package are somewhat similar to C++ friends.
-Paul
4) a number of people have explained/justified the behaviour in terms of
polymorphim. I disagree - in my opinion, - encapsulation is a compile time
issue - its about the compiler enforcing rule of visibility - so it's
determined by the object's static type and its package. Polymorphism is
purely a run-time thing - not the realm of the compiler at all. It seems to
me that fact that the compiler allows access to a protected method on an
object outside the pacakge is an odd exception to the general rule, and the
fact that the subtype is a related type is not a strong enough reason to
justify it.
The protected qualifier the caller is coming in by is the one on the superclass
in the same package. The implementation happens to be provided by some other
class which seems irrelevant to me. can something in animal call an
implementation of Animal.walk()? Yes, Animal.walk() is accessable to
things the animal package.
-Paul