Java Reference ยท Object-Oriented Concepts
Method overriding in Java
The rules of Java method overriding: signatures, covariant returns, access, checked exceptions, @Override, super calls, and what cannot be overridden.
A subclass overrides an inherited instance method when it declares a method with the same name and the same parameter types. When the method is called on an object of the subclass, the subclass version runs, even if the variable has the superclass type. Overriding is how a class customizes inherited behavior and the basis of polymorphism.
Always mark an override with @Override. The annotation makes the compiler check that you really override something, so a typo or a changed parameter type becomes a compile error instead of a silent new method.
class Base { Object make() { return "base"; } } class Derived extends Base { @Override String make() { return "derived"; } // covariant return type }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Rules
| Aspect | Requirement |
|---|---|
| Name and parameters | Identical parameter types (generics after erasure aside). Different types make an overload, not an override. |
| Return type | Same, or a subtype for reference types (covariant). Primitives must match exactly. |
| Access | Same or wider: a protected method may become public, never the reverse. |
| Checked exceptions | May throw fewer or narrower checked exceptions; no new or broader ones. Unchecked exceptions are unrestricted. |
static |
Static methods are hidden, not overridden. An instance method cannot override a static one or vice versa. |
private |
Not inherited, so a same-named method in the subclass is a new method. |
final |
A final method cannot be overridden; a final class cannot be extended. |
Usage notes
- Use
@Overrideevery time. It also works for interface methods andrecordaccessors. super.method()calls the parent version; use it to extend rather than replace behavior.Interface.super.method()selects a specific default method.- Overriding
equalsrequireshashCodetoo, andtoStringis overridden almost always. See the equals and hashCode contract. - Fields are not overridden. A same-named field in a subclass hides the parent's field.
- Do not call overridable methods from constructors: the override may run before the subclass state exists (initialization order).
- Liskov substitution: an override should keep the promises of the parent method, otherwise code using the supertype breaks in surprising ways.
- Since Java 5 an overriding method may also have a narrower generic return type; bridge methods generated by the compiler make this work at the bytecode level.