Covariant returns
A covariant return lets an override return a more specific type than the parent method. Animal copy() can become Dog copy() in a subclass.
Detailed Explanation
Callers of a parent method must be able to treat the result as the parent return type. Returning a subtype still satisfies that: every Dog is an Animal. Java has allowed this on overrides since Java 5. Before that, Dog.copy() had to return Animal, and callers who already had a Dog had to cast.
The rule is only about the return type. The method name and parameters must still match, or you are overloading, not overriding. Primitive returns cannot change (int cannot become long). void cannot become a type. For reference types, the override's return must be a subtype of the parent's return. null remains legal.
Generics interact through bridges. Because of type erasure, the compiler may emit a synthetic method that returns the erased type and forwards to your more specific method. You do not write that bridge. You do see it in stack traces and sometimes in reflection. Overriding Object clone() to return User is the classic case: the class also gets a clone that returns Object for old callers.
You reach for a covariant return when a factory-style method or copy/self method is inherited and the subclass can promise a tighter type. Fluent builders that override self() to return the concrete builder use the same idea. You avoid changing the return type when the parent type is already what callers need; a tighter type is then noise.
The benefit is fewer casts. Dog puppy = dog.copy(); compiles. The risk is that the override now commits the subclass to returning that exact family of types. A later sibling class cannot easily share a helper that returns the parent type without widening again. Keep the tighter return when it matches the object's real identity (clone, copy, newBuilder). Keep the parent return when the method is producing a collaborator that may vary.
Array returns are covariant in Java as well (String[] is an Object[]), which is a separate, older language rule and a source of ArrayStoreException. Method covariant returns are checked at compile time and do not have that store hole.
Code Examples
class Animal {
Animal copy() {
return new Animal();
}
}
class Dog extends Animal {
@Override
Dog copy() { // covariant: Dog is an Animal
return new Dog();
}
}
public class Copies {
public static void main(String[] args) {
Dog original = new Dog();
Dog twin = original.copy(); // no cast
Animal also = original.copy(); // still legal: Dog is an Animal
System.out.println(twin.getClass().getSimpleName()); // prints: Dog
System.out.println(also.getClass().getSimpleName()); // prints: Dog
}
}abstract class Builder<T> {
abstract T build();
}
class User {
final String name;
User(String name) {
this.name = name;
}
}
class UserBuilder extends Builder<User> {
private String name = "anon";
UserBuilder name(String name) {
this.name = name;
return this;
}
@Override
User build() { // more specific than a raw Object-style factory
return new User(name);
}
public static void main(String[] args) {
User u = new UserBuilder().name("Ira").build();
System.out.println(u.name); // prints: Ira
}
}Common Gotchas
A different parameter list is an overload
`copy(int unused)` does not override `copy()` and the return type rule does not apply.
Bridges appear under reflection
`getDeclaredMethods` may show two `copy` methods, one returning the parent type.
Primitives are invariant
you cannot override `Number id()` with `int id()` or `int id()` with `long id()`.
Do not confuse this with array covariance
assigning a `String[]` to `Object[]` and storing an `Integer` throws at runtime.