which are overridden, not hidden.
Hiding fields is allowed because implementors of existing superclasses must be free to add new public or
protected fields without breaking subclasses. If the language forbade using the same field name in a
superclass and a subclass, adding a new field to an existing superclass could potentially break any subclasses
already using those names.
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
superclass'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 subclass 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 methods
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 discussion of this until Chapter 18.
3.3.5. Hiding Static Members
Static members within a classwhether fields or methodscannot be overridden, 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 to access a static member then, as with instance fields, static members are
always accessed based on the declared type of the reference, not the type of the object referred to.
3.3.6. The super Keyword
The super keyword is available in all non-static methods of a class. In field access and method invocation,
super acts as a reference to the current object as an instance of its superclass. Using super is the only case
in which the type of the reference governs selection of the method implementation to be used. An invocation
of super.method always uses the implementation of method the superclass defines (or inherits). It does
not use any overriding implementation of that method further down the class hierarchy. Here is an example
that shows super in action:
class Base {
/* return the class name /
protected String name() {
return "Base";
}
}
class More extends Base {