What is the difference between overloading and overriding?
Answer
Method Overloading and Method Overriding are both ways to have multiple methods with the same name, but they serve different purposes: Overloading (compile-time polymorphism): defining multiple methods with the SAME name but DIFFERENT parameter lists within the SAME class. The compiler selects the right method based on argument types and count: class MathHelper { public int multiply(int a, int b) { return a * b; } public double multiply(double a, double b) { return a * b; } public int multiply(int a, int b, int c) { return a * b * c; } // INVALID -- only return type differs (not allowed!): // public long multiply(int a, int b) { return (long)a * b; } } MathHelper m = new MathHelper(); m.multiply(2, 3); // int version m.multiply(2.0, 3.0); // double version m.multiply(2, 3, 4); // 3-param version. Overriding (runtime polymorphism): redefining a method from a PARENT class in a CHILD class with the SAME name and SAME parameter list: class Logger { public void log(String message) { System.out.println("[INFO] " + message); } } class DebugLogger extends Logger { @Override // Annotation ensures proper override -- compiler error if wrong public void log(String message) { System.out.println("[DEBUG] " + Thread.currentThread().getName() + ": " + message); } } Logger logger = new DebugLogger(); logger.log("test"); // Calls DebugLogger.log() -- runtime dispatch. Key differences: Overloading = same class, different signature, compile-time; Overriding = parent-child, same signature, runtime. @Override annotation (Java) catches errors where you think you're overriding but aren't.