Interview Q&A · Java · 4 questions in this topic
Overloading vs Overriding — Java Interview Q&A
Overloading is compile-time: same name, different parameters, one class. Overriding is runtime: same signature, subclass replaces superclass behaviour. Part of Methods & Constructors · 294 questions in the Java bank.
What is method overloading?
Answer
Overloading means declaring two or more methods with the same name but different parameter lists (different count, types or order) in the same class — or across a superclass/subclass pair. The compiler picks the version to call at compile time from the argument list, which is why overloading is called static polymorphism or static binding.
Key points
- 1Only the parameter list distinguishes overloads — changing just the return type is a compile error.
- 2Resolution happens at compile time using the declared (reference) types.
- 3Common examples: constructors, System.out.println, String.valueOf.
Example
int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; }
What is method overriding?
Answer
Overriding means a subclass redefines an inherited instance method with the same signature, giving the same call site different behaviour depending on the actual object type. The version that runs is chosen at runtime from the object’s real class — dynamic dispatch — which is why overriding is called runtime polymorphism.
Key points
- 1Rules: same name and parameters; the return type must be the same or a subtype (covariant return, since Java 5).
- 2Access cannot be narrowed (a public method stays public); checked exceptions cannot be widened.
- 3static, final and private methods are never overridden — static ones are hidden, the rest are simply not inherited as overridable.
Example
class Animal { void speak() { System.out.println("..."); } } class Dog extends Animal { @Override void speak() { System.out.println("woof"); } }
What is the difference between method overloading and method overriding?
Answer
Overloading reuses a name with different parameters and is resolved by the compiler; overriding redefines the same signature in a subclass and is resolved at runtime. One is about convenience (many ways to call one name), the other is about polymorphism (one call, behaviour chosen by the object).
Key points
- 1Scope: overloading lives in one class; overriding spans superclass and subclass.
- 2Binding: overloading is static/compile-time; overriding is dynamic/runtime.
- 3Signature: overloading requires different parameters; overriding requires identical parameters (return may be covariant).
- 4Inheritance is required for overriding, optional for overloading.
Difference between overriding and overloading — which is which in an interview answer?
Answer
Give the one-line version first, then the decisive detail. "Overloading: same method name, different parameter list, picked by the compiler. Overriding: same signature redefined in a subclass, picked at runtime by the object’s type." Then add the discriminator interviewers listen for: return type alone never distinguishes an overload, while an override may narrow the return type (covariant) but never the access.
Key points
- 1Overloading → static polymorphism, early binding, compile-time.
- 2Overriding → runtime polymorphism, late binding, dynamic dispatch.
- 3private, static and final methods cannot be overridden.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission