If a method declaration d_1 with return type R_1 overrides or hides the declaration of another method d_2 with return type R_2, then d_1 must be return-type-substitutable (§8.4.5) for d_2, or a compile-time error occurs.
This rule allows for covariant return types - refining the return type of a method when overriding it.
If R_1 is not a subtype of R_2, then a compile-time unchecked warning occurs, unless suppressed by @SuppressWarnings (§9.6.4.5).
A method that overrides or hides another method, including methods that implement abstract methods defined in interfaces, may not be declared to throw more checked exceptions than the overridden or hidden method.
In this respect, overriding of methods differs from hiding of fields (§8.3), for it is permissible for a field to hide a field of another type.
More precisely, suppose that B is a class or interface, and A is a superclass or superinterface of B, and a method declaration m_2 in B overrides or hides a method declaration m_1 in A. Then:
- If
m_2has athrowsclause that mentions any checked exception types, thenm_1must have athrowsclause, or a compile-time error occurs. - For every checked exception type listed in the
throwsclause ofm_2, that same exception class or one of its supertypes must occur in the erasure (§4.6) of thethrowsclause ofm_1; otherwise, a compile-time error occurs. - If the unerased
throwsclause ofm_1does not contain a supertype of each exception type in thethrowsclause ofm_2(adapted, if necessary, to the type parameters ofm_1), then a compile-time unchecked warning occurs, unless suppressed by@SuppressWarnings(§9.6.4.5).
It is a compile-time error if a class or interface C has a member method m_1 and there exists a method m_2 declared in C or a superclass or superinterface of C, A, such that all of the following are true:
m_1andm_2have the same name.m_2is accessible (§6.6) from C.- The signature of
m_1is not a subsignature (§8.4.2) of the signature ofm_2as a member of the supertype of C that names A. - The declared signature of
m_1or some methodm_1overrides (directly or indirectly) has the same erasure as the declared signature ofm_2or some methodm_2overrides (directly or indirectly).
These restrictions are necessary because generics are implemented via erasure. The rule above implies that methods declared in the same class with the same name must have different erasures. It also implies that a class or interface cannot implement or extend two distinct parameterizations of the same generic interface.
The access modifier of an overriding or hiding method must provide at least as much access as the overridden or hidden method, as follows:
- If the overridden or hidden method is
public, then the overriding or hiding method must bepublic; otherwise, a compile-time error occurs. - If the overridden or hidden method is
protected, then the overriding or hiding method must beprotectedorpublic; otherwise, a compile-time error occurs. - If the overridden or hidden method has package access, then the overriding or hiding method must not be
private; otherwise, a compile-time error occurs.
Note that a private method cannot be overridden or hidden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.
Example 8.4.8.3-1. Covariant Return Types
The following declarations are legal in the Java programming language from Java SE 5.0 onwards:
class C implements Cloneable {
C copy() throws CloneNotSupportedException {
return (C)clone();
}
}
class D extends C implements Cloneable {
D copy() throws CloneNotSupportedException {
return (D)clone();
}
}
The relaxed rule for overriding also allows one to relax the conditions on abstract classes implementing interfaces.
Example 8.4.8.3-2. Unchecked Warning from Return Type
Consider:
class StringSorter {
// turns a collection of strings into a sorted list
List toList(Collection c) {...}
}
and assume that someone subclasses StringSorter:
class Overrider extends StringSorter {
List toList(Collection c) {...}
}
Now, at some point the author of StringSorter decides to generify the code:
class StringSorter {
// turns a collection of strings into a sorted list
List<String> toList(Collection<String> c) {...}
}
An unchecked warning would be given when compiling Overrider against the new definition of StringSorter because the return type of Overrider.toList is List, which is not a subtype of the return type of the overridden method, List<String>.
Example 8.4.8.3-3. Incorrect Overriding because of throws
This program uses the usual and conventional form for declaring a new exception type, in its declaration of the class BadPointException:
class BadPointException extends Exception {
BadPointException() { super(); }
BadPointException(String s) { super(s); }
}
class Point {
int x, y;
void move(int dx, int dy) { x += dx; y += dy; }
}
class CheckedPoint extends Point {
void move(int dx, int dy) throws BadPointException {
if ((x + dx) < 0 || (y + dy) < 0)
throw new BadPointException();
x += dx; y += dy;
}
}
The program results in a compile-time error, because the override of method move in class CheckedPoint declares that it will throw a checked exception that the move in class Point has not declared. If this were not considered an error, an invoker of the method move on a reference of type Point could find the contract between it and Point broken if this exception were thrown.
Removing the throws clause does not help:
class CheckedPoint extends Point {
void move(int dx, int dy) {
if ((x + dx) < 0 || (y + dy) < 0)
throw new BadPointException();
x += dx; y += dy;
}
}
A different compile-time error now occurs, because the body of the method move cannot throw a checked exception, namely BadPointException, that does not appear in the throws clause for move.
Example 8.4.8.3-4. Erasure Affects Overriding
A class cannot have two member methods with the same name and type erasure:
class C<T> {
T id (T x) {...}
}
class D extends C<String> {
Object id(Object x) {...}
}
This is illegal since D.id(Object) is a member of D, C<String>.id(String) is declared in a supertype of D, and:
- The two methods have the same name,
id C<String>.id(String)is accessible toD- The signature of
D.id(Object)is not a subsignature of that ofC<String>.id(String) - The two methods have the same erasure
Two different methods of a class may not override methods with the same erasure:
class C<T> {
T id(T x) {...}
}
interface I<T> {
T id(T x);
}
class D extends C<String> implements I<Integer> {
public String id(String x) {...}
public Integer id(Integer x) {...}
}
This is also illegal, since D.id(String) is a member of D, D.id(Integer) is declared in D, and:
- The two methods have the same name,
id D.id(Integer)is accessible toD- The two methods have different signatures (and neither is a subsignature of the other)
D.id(String)overridesC<String>.id(String)andD.id(Integer)overridesI.id(Integer)yet the two overridden methods have the same erasure