Method overriding in Java happens when a subclass provides its own implementation of a method that it inherits from a superclass or interface, using the same name, the same parameter types and a compatible return type. The version that runs is chosen at runtime from the actual type of the object, not from the type of the variable. Overloading is different: it means several methods with the same name but different parameter lists in one class, and the compiler picks one at compile time.
If you are new to inheritance, think of overriding as "replace the behaviour I inherited" and overloading as "offer several ways to call the same name".
A subclass overrides a method by declaring one with the same signature as an instance method it inherits. When you call that method on an object, the JVM looks at the real class of the object and runs the most specific version.
The variables a, b and c all have the type Animal, yet each call runs the method of the object stored in it. This is called dynamic method dispatch or runtime polymorphism.
The compiler checks these rules when you override an instance method:
| Rule | What it means |
|---|---|
| Same name and parameter types | The signature must match exactly, otherwise you are overloading or just adding a new method. |
| Return type | Must be the same, or a subtype of the original return type (a covariant return). |
| Access level | Can stay the same or become wider (protected to public), never narrower. |
| Checked exceptions | The override may throw fewer or narrower checked exceptions, never new or broader ones. |
Not final, static or private | final methods cannot be overridden; static and private methods are not inherited as instance methods. |
Here Circle.copy() is public instead of protected and returns Circle instead of Shape. Both changes are legal.
@Override is optional, but it asks the compiler to confirm that the method really overrides something. Without it, a typo silently creates a new method.
If you add @Override to the first equals, the compiler reports an error because equals(Report) does not override anything in Object. This is one of the most common real-world bugs in beginner code, and the annotation catches it for free.
Sometimes you want to extend the inherited behaviour rather than replace it. Use super.methodName() to call the superclass version.
TimestampLogger adds a time to the message and then hands it to the original method.
The two are often confused because both reuse a method name. The key difference is who decides which method runs, and when.
| Overriding | Overloading | |
|---|---|---|
| Where | Subclass and superclass | Usually the same class |
| Signature | Must be identical | Parameter list must differ |
| Resolved | At runtime, by object type | At compile time, by argument types |
| Return type | Same or covariant | Can be anything |
| Purpose | Change inherited behaviour | Offer several ways to call |
new Printer().print(5) calls the int version because the compiler sees an int argument. Nothing about the object type is involved.
static method with the same signature, the method called depends on the variable type at compile time.No. Declaring a static method with the same signature in a subclass hides the parent method. The call is resolved at compile time from the reference type, so there is no runtime polymorphism.
No. A private method is not visible to subclasses, so a method with the same name is unrelated. A final method is explicitly closed against overriding and a subclass that tries gets a compile error.
It is not required, but it is strongly recommended. It makes the compiler verify the override and prevents bugs from misspelt names or wrong parameter types.
Only to a subtype of the original return type (a covariant return). For primitive return types the type must match exactly.
Overriding replaces an inherited instance method in a subclass, keeps the same signature, and is chosen at runtime from the object's real class. Overloading gives one name several parameter lists and is resolved by the compiler. Follow the access, return type and exception rules, always add @Override, use super when you want to extend the parent behaviour, and remember that static methods, fields and private methods are never overridden.