What is the difference between method overloading and overriding?
Options
- Overload: same name, new params. Override: subclass replaces (Correct option)
- They are just two names for the very same feature
- Overloading needs inheritance; overriding works in one class
- Overriding changes the parameters; overloading the return type
Answer
Overloading: same name, different parameters in the same class. Overriding: a subclass replaces a parent's method with the same signature
Why
Overloading: several methods with the same name but different parameter lists, e.g. print(int) and print(String). The compiler picks the one that matches at compile time.
Overriding: a subclass provides its own version of an inherited method with the same name and parameters, usually with @Override. The JVM chooses at runtime based on the real object type (polymorphism).
Remember
Overload = same name, different params. Override = replace in a subclass.