the superclass, because an instance of the subclass would not be usable in place of a superclass instance.
The overriding method is also allowed to change other method modifiers. The synchronized, native,
and strictfp modifiers can be freely varied because they are implementation concerns, as are any
annotationssee Chapter 15. The overriding method can be final but obviously the method it is overriding
cannotsee "Marking Methods and Classes final" on page 96 for the implications of final methods. An
instance method cannot have the same signature as an inherited static method, and vice versa. The overriding
method can, however, be made abstract, even though the superclass method was notsee "Abstract Classes
and Methods" on page 97.
A subclass can change whether a parameter in an overriding method is final; a final modifier for a
parameter is not part of the method signatureit is an implementation detail. Also, the overriding method's
throws clause can be different from that of the superclass method's as long as every exception type listed in
the overriding method is the same as or a subtype of the exceptions listed in the superclass's method. That is,
each type in the overriding method's tHRows clause must be polymorphically compatible with at least one of
the types listed in the throws clause of the supertype's method. This means that the throws clause of an
overriding method can have fewer types listed than the method in the superclass, or more specific types, or
both. The overriding method can even have no tHRows clause, which means that it results in no checked
exceptions. Exceptions and tHRows clauses are described in detail in Chapter 12.
3.3.2. Hiding Fields
Fields cannot be overridden; they can only be hidden. If you declare a field in your class with the same name
(regardless of type) as one in your superclass, that other field still exists, but it can no longer be accessed
directly by its simple name. You must use super or another reference of your superclass's type to access it.
We show you an example in the next section.
3.3.3. Accessing Inherited Members
When a method accesses an object's member that has been redefined in a subclass, to which member will the
method referthe superclass member or the subclass member? The answer to that depends on the kind of
member, its accessibility, and how you refer to it.
When you invoke a method through an object reference, the actual class of the object governs which
implementation is used. When you access a field, the declared type of the reference is used. The following
example will help explain:
class SuperShow {
public String str = "SuperStr";
public void show() {
System.out.println("Super.show: " + str);
}
}
class ExtendShow extends SuperShow {
public String str = "ExtendStr";
public void show() {
System.out.println("Extend.show: " + str);
}
public static void main(String[] args) {
ExtendShow ext = new ExtendShow();
SuperShow sup = ext;