Interview Q&A · Java · 4 questions in this topic
Covariant Return Types — Java Interview Q&A
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
- 1Only the return type may narrow; parameter types must still match the overridden method exactly.
- 2The compiler may emit a synthetic bridge method so callers compiled against the supertype still bind correctly.
- 3Arrays are covariant by a separate rule: a String[] is also an Object[], which is why runtime array-store checks exist.
Example
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
- 1The overriding return type must be a subtype of the original.
- 2Parameters must match exactly, or it is an overload.
- 3Access modifiers may widen, never narrow.
- 4The compiler emits a bridge method to preserve the old signature.
Example
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
- 1Signatures include parameter types, so a change means overloading.
- 2The inherited method still exists and still dispatches.
- 3Mixing the two silently splits behaviour by static type.
- 4@Override makes the mistake a compile error.
What is a bridge method and why does it appear?
Answer
When a covariant return or a generic override changes a signature in the subclass, the compiler generates a synthetic bridge method with the superclass signature that casts and delegates to the specialised one. That is what keeps polymorphism working for callers compiled against the old type. Bridge methods are invisible in source, marked synthetic in reflection, and their cast is the reason a raw-type misuse can throw ClassCastException at a point that looks innocent.
Key points
- 1Bridge methods are compiler-generated synthetic methods.
- 2They cast the arguments or results to the specialised signature.
- 3They exist for binary compatibility and polymorphism.
- 4Unchecked raw-type use surfaces as a cast failure inside the bridge.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission