What is the difference between compile-time polymorphism (method overloading) and runtime polymorphism (method overriding) in Java?
Compile-time polymorphism happens through method overloading — multiple methods with the same name but different parameter lists in the same class — and the compiler decides which one to call based on the argument types at compile time. Runtime polymorphism happens through method overriding, where a subclass provides its own implementation of a method already defined in its parent, and which version actually runs is decided at runtime based on the real object behind the reference, not its declared type. A classic example is a `PaymentMethod` reference that could point to a `CreditCard` or a `UPI` object at runtime — the same line of code, `method.pay(amount)`, calls a different implementation depending on what `method` actually references when the line executes. Overloading is resolved statically by the compiler using the method signature; overriding is resolved dynamically by the JVM using the object's actual class, which is why it's also called dynamic dispatch.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response