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

Covariant Return Types — Java Interview Q&A

All Java topics

How an overriding method may narrow its return type to a subtype since Java 5. Part of OOP Concepts · 294 questions in the Java bank.

Covariant returns let a subclass make the API more precise without breaking callers: an override may return a subtype, so code written against the parent still compiles while callers holding the subclass see the richer type without a cast. This is especially useful for builders and copy methods, where a fluent subclass should return the subclass. Primitives have no subtype relationship, so the rule applies to reference types; the compiler enforces it as part of the override check rather than through a cast at runtime.

What is a covariant return type?

Answer

A covariant return type is a narrowed return type in an overriding method: since Java 5 the override may return a subtype of the original return type instead of the identical type. This stays type-safe because callers expecting the parent type still receive a compatible object. Note for readers of the source PDF: this question is mis-numbered as 59 even though it sits among the OOP questions, so the number is preserved here for traceability.

Key points

  1. 1Only the return type may narrow; parameter types must still match the overridden method exactly.
  2. 2The compiler may emit a synthetic bridge method so callers compiled against the supertype still bind correctly.
  3. 3Arrays are covariant by a separate rule: a String[] is also an Object[], which is why runtime array-store checks exist.

Example

Example.javajava
class A {
    A doSomething() {
        return new A();
    }
}
class B extends A {
    @Override
    B doSomething() {
        return new B();
    }
}

💡 Mention that Java 1.4 and earlier demanded identical return types, so narrowing is the modern rule to state.

What is a covariant return type?

Answer

An overriding method may narrow its return type to a subtype of the superclass method’s return type — a feature added in Java 5. So a clone() or builder method can declare the concrete type instead of the base type, sparing callers a cast. The parameter list must still match exactly, and access cannot become more restrictive; only the return type is allowed to specialise.

Key points

  1. 1The overriding return type must be a subtype of the original.
  2. 2Parameters must match exactly, or it is an overload.
  3. 3Access modifiers may widen, never narrow.
  4. 4The compiler emits a bridge method to preserve the old signature.

Example

Example.javajava
class Base { Base copy() { return new Base(); } }
class Sub extends Base {
    @Override Sub copy() { return new Sub(); }  // covariant
}

Can you narrow a parameter type when overriding?

Answer

No. Changing a parameter type creates an overload, not an override, because the method signature includes the parameter types. The compiler will happily accept the new method and leave the inherited one in place, so calls through the supertype reference reach the original method while calls on the subtype may reach the new one — behaviour that looks like broken polymorphism. Parameters are invariant; only return types are covariant.

Key points

  1. 1Signatures include parameter types, so a change means overloading.
  2. 2The inherited method still exists and still dispatches.
  3. 3Mixing the two silently splits behaviour by static type.
  4. 4@Override makes the mistake a compile error.

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