HuntExams Academy logo
JavaBeginner#oop#polymorphism

What is method overloading vs overriding?

Answer

Overloading: same method name, different parameter list, within the same class — resolved at compile time. Overriding: subclass redefines a parent method with the same signature — resolved at runtime (dynamic dispatch).

Example

// Overloading
void add(int a, int b) {}
void add(double a, double b) {}
// Overriding
class Parent { void greet(){ System.out.println("Hi"); } }
class Child extends Parent { @Override void greet(){ System.out.println("Hey"); } }

Detailed explanation

Method overloading and method overriding are two ways Java supports polymorphism, but they solve different problems. Overloading lets a method name represent multiple operations with different parameter lists, such as add(int a, int b) and add(double a, double b). Overriding lets a subclass provide its own implementation of an inherited instance method, such as Child replacing Parent's greet() behaviour. The main interview distinction is that overload selection happens at compile time, while dispatch to an overridden implementation happens at runtime.

For overloading, the parameter lists must differ in the number, types, or order of parameter types. Changing only parameter names or the return type does not create a valid overload. Overloads commonly appear in the same class, but a subclass can also overload an inherited method. The compiler selects an applicable overload using the declared types of arguments and Java's conversion rules, including widening, boxing, and varargs. For example, add(2, 3) selects the int version, whereas add(2.0, 3.0) selects the double version. Some calls can be ambiguous, causing a compilation error.

For overriding, the subclass method must match the inherited method's name and parameter types, with the same return type or a covariant reference return type. Consider Parent p = new Child(); followed by p.greet(). The compiler checks that greet() is available through Parent, but runtime dispatch executes Child's implementation because the object is a Child. This enables code written against a parent class or interface to work with different implementations. Use @Override so the compiler detects mistakes in an intended override.

An overriding method cannot reduce access visibility or declare broader checked exceptions than the overridden method permits. A final method cannot be overridden, a private method is not inherited, and a static method is hidden rather than overridden. Constructors can be overloaded but cannot be overridden. Interviewers usually check whether you can distinguish reference type from object type and predict output correctly: the declared types determine overload selection, and the actual object's class determines which override of the selected instance method runs.

Key points to remember

  • Overloading uses the same method name with different parameter lists; selection occurs at compile time.
  • Overriding supplies a subclass implementation of an inherited instance method; dispatch occurs at runtime.
  • Return type alone cannot distinguish overloads; overriding allows covariant reference return types.
  • Overriding cannot reduce visibility or broaden the permitted checked exceptions.
  • Static methods are hidden, final methods cannot be overridden, and constructors support only overloading.

Common mistakes candidates make

  • Claiming that changing only the return type creates an overloaded method.
  • Using the argument's runtime class to predict overload selection instead of its declared type.
  • Calling a same-named static or private subclass method an override.

Likely follow-up questions

Can overloading and overriding happen together?

Yes, a subclass can override one inherited method and introduce another overload with different parameters. The compiler first selects a signature, then runtime dispatch chooses the overriding implementation for that selected instance method.

What is a covariant return type?

An overriding method may return a subtype of the original reference return type. For example, if Parent.create() returns Animal, Child.create() may return Dog when Dog extends Animal; this rule does not allow changing int to long.

What happens when static methods have the same signature in parent and child classes?

The child's static method hides the parent's method rather than overriding it. Selection depends on the compile-time type or class qualifier, not the runtime object; call static methods through class names for clarity.

Interview tip: Start with compile-time selection versus runtime dispatch, show one example of each, and explain why Parent p = new Child() calls the child's overridden method.

Related Interview Questions

1
JavaBeginner#keywords

What is the final keyword?

Open
2
JavaIntermediate#memory#jvm

Explain Java memory model: heap vs stack.

Open
3
JavaIntermediate#gc#memory

What is garbage collection?

Open