Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 30

Method Overloading vs Overriding in Java

In this lesson: Difference between method overloading and overriding in Java — rules, comparison table and examples with output. In Hindi & English.

Two similar-sounding ideas

Overloading and overriding sound alike but are very different. Overloading is about having several methods with the same name in one class (different parameters). Overriding is about a child class replacing a parent's method. Both are forms of polymorphism.

💡 Overload = same name, different parameters, same class. Override = same signature, child replaces parent.

Method overloading

class Calc {
    int add(int a, int b) { return a + b; }
    double add(double a, double b) { return a + b; }
    int add(int a, int b, int c) { return a + b + c; }
}
// use:
Calc c = new Calc();
System.out.println(c.add(2, 3));       // 5
System.out.println(c.add(2.5, 1.5));   // 4.0
System.out.println(c.add(1, 2, 3));    // 6
5
4.0
6

Three methods share the name add but differ in parameters. Java picks the matching one from the arguments — this is overloading, decided at compile time.

Method overriding

class Animal {
    void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
    @Override
    void sound() { System.out.println("Woof"); }
}
// use:
Animal a = new Dog();
a.sound();       // Woof (child's version)
Woof

Dog overrides sound() with the same signature. Even though a is typed as Animal, the actual object is a Dog, so the child's version runs — decided at runtime.

Compile-time vs runtime

  • Overloading = compile-time polymorphism. The compiler chooses the method from the argument types.
  • Overriding = runtime polymorphism. The JVM chooses based on the actual object at run time.

This is why overloading is also called static, and overriding dynamic, polymorphism.

Side-by-side comparison

PointOverloadingOverriding
WhereSame classParent and child
ParametersMust differMust be same
Method nameSameSame
ResolvedCompile timeRuntime
Needs inheritanceNoYes

Common mistakes

  • Only changing the return type. Overloading needs different parameters, not just a different return type.
  • Different signature when overriding. The child method must match exactly, or it becomes an overload.
  • Skipping @Override. It is optional but catches signature mistakes early.
  • Expecting overriding without inheritance. Overriding needs a parent-child relationship.

Summary

  • Overloading: same name, different parameters, in one class (compile time).
  • Overriding: same signature, child replaces parent's method (runtime).
  • Overloading needs no inheritance; overriding does.
  • Overloading is static polymorphism; overriding is dynamic.
  • Use @Override to catch mistakes when overriding.

Next, revise inheritance, or reuse methods safely with Java methods.

Frequently Asked Questions

What is the difference between overloading and overriding?
Overloading is having several methods with the same name but different parameters in the same class. Overriding is a child class giving its own version of a parent method with the same name and parameters. One is same-class variety; the other is parent-child replacement.
What is method overloading?
Overloading means multiple methods share a name but differ in the number or type of parameters, like add(int, int) and add(double, double). Java picks the right one from the arguments at compile time.
What is method overriding?
Overriding is when a subclass redefines an inherited method with the exact same signature, so the child's version runs for child objects. It is the basis of runtime polymorphism.
Is overloading compile-time or runtime?
Overloading is resolved at compile time (also called static polymorphism), because the compiler chooses the method from the argument types. Overriding is resolved at runtime (dynamic polymorphism), based on the actual object.
What is the @Override annotation for?
@Override tells the compiler you intend to override a parent method. If your signature does not actually match a parent method, the compiler flags an error — a helpful safety check against typos.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicfinal, finally, finalize Your progress: 30 / 61 (49%) · Lesson 31 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free