What is the difference between Method Overloading and Method Overriding?
Last updated
Overloading is defining multiple methods with the same name but different parameter lists in the same class (compile-time), while overriding is redefining an inherited method with the same signature in a subclass to change behavior (runtime).
Method overloading:
Same name, different parameter types/count/order; return type alone is not enough.
Resolved at compile time by the static argument types (static polymorphism).
Lives within one class (or an inheritance hierarchy) and is about convenience.
Method overriding:
Same signature as a superclass method, redefined in a subclass.
Resolved at runtime by the object's actual type (dynamic dispatch).
Requires an inheritance relationship; enables substitutability.
Quick contrast: Overloading = "same name, different arguments, same class." Overriding = "same signature, different class (subclass), new behavior."
[lang:java]
class Printer {
void print(int i) {} // overload
void print(String s) {} // overload
}
class Base { void speak() { System.out.println("base"); } }
class Child extends Base {
@Override void speak() { System.out.println("child"); } // override
}
- https://www.reddit.com/r/developersIndia/comments/1ktgmfk/software_development_interview_preparation/
- https://www.reddit.com/r/programming/comments/1m2ff3j/casey_muratori_the_big_oops_anatomy_of_a/
- https://www.reddit.com/r/SoftwareEngineering/comments/1ijqgpb/is_the_o_in_solid_still_relevant_or_just_a_relic/
- https://www.reddit.com/r/NorthropGrumman/comments/1bt1ug3/monthly_employmentcorporate_questions_and/
- https://www.interviewbit.com/oops-interview-questions/