Interview Q&A · Java · 4 questions in this topic

Overloading vs Overriding — Java Interview Q&A

All Java topics

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

  1. 1Only the parameter list distinguishes overloads — changing just the return type is a compile error.
  2. 2Resolution happens at compile time using the declared (reference) types.
  3. 3Common examples: constructors, System.out.println, String.valueOf.

Example

Example.javajava
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

  1. 1Rules: same name and parameters; the return type must be the same or a subtype (covariant return, since Java 5).
  2. 2Access cannot be narrowed (a public method stays public); checked exceptions cannot be widened.
  3. 3static, final and private methods are never overridden — static ones are hidden, the rest are simply not inherited as overridable.

Example

Example.javajava
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

  1. 1Scope: overloading lives in one class; overriding spans superclass and subclass.
  2. 2Binding: overloading is static/compile-time; overriding is dynamic/runtime.
  3. 3Signature: overloading requires different parameters; overriding requires identical parameters (return may be covariant).
  4. 4Inheritance is required for overriding, optional for overloading.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits