Interview replay

Full round replay — overloading and overriding

10 minjunior08 yrs

Timed verbal replay with pass/fail criteria per follow-up.

How to run this

Answer out loud, timed. Do not read the entry first. Then compare against "The Answer" and "Interviewer's Next Move" and mark yourself.

The opener

What is the difference between overloading and overriding?

Budget: 45 seconds. Going long here is itself a fail signal.

Follow-ups

  1. 1. “Can you override a static method?

    Testing: Hiding vs overriding, and whether they know the mechanism.

    Scoring

    Pass: No — a same-signature static hides the parent's, and hiding is resolved from the reference type at compile time. invokestatic never consults an object.

    Fail: 'Yes' — or 'no' with no idea what happens instead.

  2. 2. “Are fields polymorphic?

    Testing: Almost nobody has tried this.

    Scoring

    Pass: No. A subclass field with the same name shadows the parent's; both exist in the object and which you read depends on the declared type of the expression. Same rule as static methods.

    Fail: 'Yes, if they're protected.'

  3. 3. “Object o = "hello"; describe(o); with overloads for Object and String. Which runs?

    Testing: The single clearest test of whether they have the rule.

    Scoring

    Pass: describe(Object). Overload resolution is a compile-time decision from the declared type; the runtime type never enters into it. A cast changes what the compiler sees and so changes the answer.

    Fail: 'describe(String), because it's really a String.'

  4. 4. “And describe(null)?

    Testing: Most-specific rule, and the ambiguity case.

    Scoring

    Pass: describe(String) — the most specific applicable overload. If two were equally specific, say String and Integer, it would not compile at all; you disambiguate by casting the null.

    Fail: 'NullPointerException' or 'describe(Object)'.

  5. 5. “What may an override change about the signature?

    Testing: The four rules.

    Scoring

    Pass: Parameters: nothing. Return: may narrow to a subtype since Java 5. Access: may widen, never narrow. Checked exceptions: may narrow or drop, never broaden. And the parent method must not be private, static or final.

    Fail: Gets return covariance or the exception direction backwards.

  6. 6. “Why does widening beat boxing?

    Testing: Three-phase resolution and why the order exists.

    Scoring

    Pass: Resolution runs widening, then boxing, then varargs, stopping at the first phase with a match. The order exists so that pre-Java-5 code kept binding to exactly the methods it always did when boxing and varargs were added.

    Fail: 'It's faster' or 'declaration order'.

  7. 7. “Your equals is being ignored and a HashSet has duplicates. What happened?

    Testing: Can they connect the rule to the commonest real bug it causes?

    Scoring

    Pass: equals(MyType) is an overload, not an override, so collections calling equals(Object) get Object's identity version. @Override would have made it a compile error.

    Fail: Blames hashCode alone, or the collection.

Score yourself

7/7 — you can hold this topic at senior level 5-6 — solid; reread "Interviewer's Next Move" 0-4 — reread "Understand It" and redo the Challenge tier

← Back to What is the difference between overloading and overriding?