Java FAQ: Top Questions
12. What is the difference between method overriding and method overloading?
Method overriding and method overloading are two forms of polymorphism in Java, but they serve different purposes.
-
Method Overriding:
- Occurs when a subclass provides a specific implementation for a method already defined in its superclass.
- The method in the subclass must have the same name, return type (or covariant), and parameter list.
-
Uses the
@Override
annotation to ensure correctness. - Enables runtime polymorphism (dynamic method dispatch).
-
Method Overloading:
- Occurs when multiple methods in the same class have the same name but different parameter lists (number, type, or order).
- Resolved at compile-time (static polymorphism).
- Return type alone cannot differentiate overloaded methods.