Java Reference
In-Depth Information
If adding new fields to existing superclasses would break some unknown
number of subclasses, you'd be effectively immobilized, unable to add
public or protected fields to a superclass. Purists might well argue that
classes should have only private data, but you get to decide on your
style.
3.3.4. Accessibility and Overriding
A method can be overridden only if it is accessible. If the method is not
accessible then it is not inherited, and if it is not inherited it can't be
overridden. For example, a private method is not accessible outside its
own class. If a subclass defines a method that coincidentally has the
same signature and return type as the superclass's private method, they
are completely unrelatedthe subclass method does not override the su-
perclass's private method.
What does this mean in practice? An external invocation of the subclass
method (assuming it is accessible outside its class) results in the sub-
class implementation being invoked. This is normal behavior. But notice
that in the superclass, any invocations of the private method result in
the superclass's implementation of the method being invoked, not any
like-named method in a subclass. In short, invocations of private meth-
ods always invoke the implementation of the method declared in the
current class.
When a method is inaccessible because the superclass and subclass are
in different packages things are more complicated. We defer a discus-
sion of this until Chapter 18 .
3.3.5. Hiding Static Members
Static members within a classwhether fields or methodscannot be over-
ridden, they are always hidden. The fact that they are hidden has little
effect, howevereach static field or method should always be accessed
via the name of its declaring class, hence the fact that it gets hidden by
a declaration in a subclass is of little consequence. If a reference is used
 
Search WWH ::




Custom Search