Java Reference
In-Depth Information
The overriding methods have their own access specifiers. A subclass can
change the access of a superclass's methods, but only to provide more
access. A method declared protected in the superclass can be redeclared
protected (the usual thing to do) or declared public , but it cannot be de-
clared private or have package access. Making a method less accessible
than it was in a superclass would violate the contract of 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 modifi-
ers. 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 meth-
od it is overriding cannotsee " Marking Methods and Classes final " on
page 96 for the implications of final methods. An instance method can-
not 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 Meth-
ods " 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 signa-
tureit 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 polymorph-
ically 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 over-
riding method can have fewer types listed than the method in the super-
class, or more specific types, or both. The overriding method can even
have no tHRows clause, which means that it results in no checked excep-
tions. Exceptions and tHRows clauses are described in detail in Chapter
12 .
 
Search WWH ::




Custom Search