Sub topic 7
The Rules of Overriding
When a subclass overrides a method, the Java compiler checks it against a few rules. These rules exist for one reason. Any code that works with the parent type must keep working when it gets a subclass object instead. If an override could break that promise, the compiler rejects it.
The Four Rules
| Rule | What is allowed | What is not allowed |
|---|---|---|
| Signature | Same method name and same parameter types | Different parameter types (that makes a new method) |
| Return type | Same type, or a subtype of it | An unrelated or wider type |
| Access | Same access, or wider access | Narrower access |
| Checked exceptions | Same exceptions, fewer, or narrower ones | New or broader checked exceptions |
A signature is a method's name plus the list of its parameter types. An override must match the parent's signature exactly.
A checked exception is an exception the compiler makes you handle, such as IOException. You must catch it or declare it with throws.
Return Types and Access
A covariant return is an override that returns a subtype of the parent's return type. It is allowed because a Circle is still a Shape, so callers get what they expect.
Access modifiers control who can call a method. From most open to most closed, they are public, protected, package-private (no keyword), and private. An override can open access up but can never close it down.
class Shape {
protected Shape copy() {
return new Shape();
}
}
class Circle extends Shape {
@Override
public Circle copy() { // wider access, narrower return type
return new Circle();
}
}
public class Main {
public static void main(String[] args) {
Circle c = new Circle();
Circle twin = c.copy(); // no cast needed, thanks to the covariant return
System.out.println(twin.getClass().getSimpleName()); // Circle
}
}In this example, Circle.copy() changes two things, and both are legal:
- Access goes from
protectedtopublic, which is wider. - The return type goes from
ShapetoCircle, which is a subtype.
If Circle.copy() were package-private, the compiler would report an error: "attempting to assign weaker access privileges."
Checked Exceptions
An override may throw fewer checked exceptions than the parent. It may not add new ones.
import java.io.IOException;
class Reader {
String read() throws IOException {
return "data";
}
}
class SafeReader extends Reader {
@Override
String read() { // throws nothing: allowed
return "safe data";
}
// Not allowed. Exception is broader than IOException:
// @Override
// String read() throws Exception { return "x"; }
}
public class Main {
public static void main(String[] args) throws IOException {
Reader r = new SafeReader();
System.out.println(r.read()); // safe data
}
}The caller here only prepared for IOException. If SafeReader could throw any Exception, that caller would meet an exception it never planned for. The rule prevents that.
Checking an Override Step by Step
Exercise
A parent class has public Number value() throws IOException. Which of these overrides are legal? (a) public Integer value() (b) protected Number value() (c) public Number value() throws Exception (d) public Object value()
Show answer
Only (a) is legal. Integer is a subtype of Number, which makes it a covariant return, and dropping the exception is allowed. (b) narrows access from public to protected. (c) declares a broader checked exception. (d) returns Object, which is a wider type than Number.
Exercise
Why does Java allow an override to widen access but not narrow it?
Show answer
Code that holds a parent-type reference expects to call the method with at least the parent's access. Wider access keeps that promise. Narrower access would break it.