Java Reference
In-Depth Information
name must have different erasures. It also implies that a type declaration can-
not implement or extend two distinct invocations of the same generic interface.
The access modifier (§
6.6
) 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 be
public
; otherwise, a compile-time error occurs.
• If the overridden or hidden method is
protected
, then the overriding or hiding meth-
od must be
protected
or
public
; otherwise, a compile-time error occurs.
• If the overridden or hidden method has default (package) access, then the overrid-
ing or hiding method must
not
be
private
; otherwise, a compile-time error occurs.
Note that a
private
method cannot be hidden or overridden 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 require-
ment that the return type or
throws
clause of such a method bear any relation-
ship 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: