Java Reference
In-Depth Information
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:
Click here to view code image
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 can-
not 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 to D
• The signature of D.id(Object) is not a subsignature of that of C<String>.id(String)
• The two methods have the same erasure
Two different methods of a class may not override methods with the same erasure:
Click here to view code image
class C<T> {
Search WWH ::




Custom Search