← Back to Java

Method Overloading vs Overriding

JavaEntryjava

The Question

What is method overloading and method overriding in Java?

What a Strong Answer Covers

  • overloading = same name different parameters same class compile-time
  • overriding = subclass redefines parent method same signature runtime
  • overloading return type can differ
  • overriding return type must match or be covariant

Senior-Level Answer

Method overloading and method overriding are both forms of polymorphism but operate at different phases and on different axes.

**Method overloading** (compile-time / static polymorphism): Multiple methods in the same class share the same name but differ in their parameter list — number of parameters, parameter types, or both. The compiler determines at compile time which overload to call based on the argument types and count. Return type alone is not sufficient to distinguish overloads. Example: `print(int x)`, `print(double x)`, `print(String s)` in the same class.

**Method overriding** (runtime / dynamic polymorphism): A subclass provides its own implementation of a method that is already defined in a superclass, keeping the same method signature (name + parameter list + return type, where the return type may be a covariant subtype since Java 5). The JVM determines at runtime which implementation to call based on the actual type of the object, not the declared type of the reference. This is the mechanism that enables polymorphic behavior.

Rules for overriding: - The method signature must match. - Access modifier cannot be more restrictive than the parent's. - Cannot override `static`, `final`, or `private` methods. - The `@Override` annotation should be used — it causes a compile error if the signature doesn't match, preventing silent overload-instead-of-override bugs. - Overriding method can throw narrower (or no) checked exceptions, but not broader ones.

The key conceptual distinction: overloading is resolved by the compiler based on static type; overriding is resolved by the JVM at runtime based on the actual object type. This is why overloading is called static polymorphism and overriding is dynamic polymorphism.

Key Differences

AspectOverloadingOverriding
Also calledStatic / compile-time polymorphismDynamic / runtime polymorphism
Where definedSame classSubclass (of superclass/interface)
SignatureDifferent parameter listSame parameter list and name
Return typeCan differMust match (covariant return allowed)
Resolved byCompilerJVM at runtime
@Override annotationN/ARecommended — prevents silent bugs

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly distinguishes compile-time vs runtime resolution and gives a concrete example of each.

3/3 — Strong Answer

Also covers the overriding rules (access modifier, exceptions, covariant return), the @Override annotation, and explains why static/private/final methods cannot be overridden.

Common Mistakes

  • Thinking return type alone differentiates overloaded methods — it doesn't; the compiler uses parameter lists.
  • Attempting to override a static method — static methods are hidden (shadowed), not overridden; virtual dispatch does not apply.
  • Omitting @Override and accidentally overloading instead of overriding due to a parameter type mismatch.

Follow-Up Questions

  • What happens when you define a static method in a subclass with the same signature as a static method in the parent? — This is method hiding, not overriding. The method called depends on the declared reference type, not the runtime type.
  • Can an overriding method throw a broader checked exception than the parent method? — No — it would violate substitutability (Liskov). It can throw narrower exceptions or no checked exceptions.
  • Why can't private methods be overridden? — Private methods are not visible to subclasses; a same-signature method in a subclass is a new, independent method, not an override.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout