TCSObject-Oriented ProgrammingEasy
What is the difference between method overloading and overriding?
JavaOOPPolymorphismOverloadingOverridingInheritanceCompile-timeRuntime
Question
Explain method overloading and method overriding in Java with detailed examples, differences, use cases, and best practices.
Interview Answer
Overloading means same method name with different parameters in the same class (compile-time resolution). Overriding means subclass provides a new implementation of the same method signature (runtime polymorphism).
Explanation
Overloading improves API convenience and readability for different input shapes. Overriding enables subtype-specific behavior through inheritance and dynamic dispatch. The compiler chooses overloaded methods; JVM runtime chooses overridden method implementation by actual object type.
Key Points
- Overloading: same name, different parameter list
- Overriding: same signature, subclass implementation
- Overloading is compile-time polymorphism
- Overriding is runtime polymorphism
Common Mistakes
- Thinking return type alone can overload methods
- Confusing method hiding (static) with overriding
- Reducing visibility when overriding parent methods
Likely Follow-Up Questions
- Can constructors be overloaded and overridden?
- What is covariant return type in overriding?
- Why is @Override annotation recommended?
Interview Timing
Target speaking time: about 5 minutes.