Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

What is a covariant return type? Show an example with clone() and explain what the compiler generates.

A covariant return type means an overriding method may declare a return type that is a subtype of the overridden method's return type. For example, Point can override Object.clone(), which returns Object, with public Point clone(), so callers get a Point without a cast. Because the JVM identifies methods by their full descriptor including the return type, javac also generates a synthetic bridge method Object clone() that calls the Point version, so code compiled against Object.clone() still links. Covariance applies only to reference return types, never to parameters.

Why this question is asked

Covariant return types are a small feature that we use all the time without naming it: clone methods, builders that return their own type, and interfaces whose implementations return a more specific type. Interviewers ask about it because the answer shows whether we understand the difference between what the Java language allows and what the JVM actually executes, and bridge methods are the place where those two meet. In this answer, we will recap the rules for overriding, write a covariant clone(), look at the bridge method the compiler generates and why it must exist, and finish with what covariance does not cover.

1A Quick Recap of Overriding Rules

When a subclass declares a method with the same name and parameter types as an inherited instance method, it overrides it. Before Java 5, the return type also had to be exactly the same. Since Java 5, an override may return a subtype of the original return type. That is what covariant means here: the return type varies in the same direction as the class, getting more specific as the class gets more specific.

This is safe for callers. Code that calls the method through the parent type expects some Object, and a Point is an Object. Code that knows it holds a Point now gets the precise type and does not need a cast.

2Writing a Covariant clone()

Object.clone() is declared protected, returns Object and throws the checked CloneNotSupportedException. When we override it, we can widen the access to public, narrow the return type to our class, and drop the checked exception, since our class implements Cloneable and the exception cannot happen.

Java
final class Point implements Cloneable {
int x, y;
Point(int x, int y) { this.x = x; this.y = y; }
@Override
public Point clone() { // covariant: Point instead of Object
try {
return (Point) super.clone(); // the cast happens once, in here
} catch (CloneNotSupportedException e) {
throw new AssertionError(e); // cannot happen: we implement Cloneable
}
}
}
Point a = new Point(1, 2);
Point b = a.clone(); // no cast needed
b.x = 9;
System.out.println(a.x + " " + b.x); // 1 9
Object o = a;
System.out.println(o.equals(b)); // false: a different object

Notice that the cast moved from every caller into one place, the clone() method itself. Before covariant returns, every caller had to write (Point) a.clone(). The JDK uses the same pattern: ArrayList.clone() still returns Object for compatibility, but many newer classes return their own type.

3What the Compiler Generates: the Bridge Method

At the bytecode level, the JVM does not identify a method only by its name and parameters. It uses the full descriptor, which includes the return type. So for the JVM, Point clone() and Object clone() are two different methods, and Point's method does not override Object's at all. Code compiled against Object, such as a generic copy utility, calls the method with descriptor ()Ljava/lang/Object; and would not find our Point version.

To fix this, javac adds a second method to Point with the original signature. It is marked synthetic, because it does not appear in our source, and bridge, because it only bridges one signature to another. Its body simply calls the covariant method.

Java
// javap -p Point (abridged): two clone methods in the class file
// public Point clone();
// public java.lang.Object clone(); // flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC
// The bridge behaves as if javac had written:
public Object clone() {
return this.clone(); // calls the Point-returning version (invokevirtual Point.clone:()LPoint;)
}
Step throughHow each call reaches Point.clone()
Caller using ObjectBridge Object clone()Point clone()Caller using Point
invokevirtual Object.clone()
invokevirtual Point.clone()
Point
Object (a Point)
invokevirtual Point.clone()
Point

6 messages in order. Play them, or step through them one at a time.

The bridge keeps the old descriptor working while Point-aware callers skip it.

Now there are two entry points. A call compiled as Object.clone() lands on the bridge, which forwards to the real method. A call compiled against Point uses the covariant method directly. Callers compiled before Point existed still link and still get the overriding behaviour, which is what the language promises.

4Seeing the Bridge Method at Run Time

Reflection shows both methods, which sometimes surprises frameworks and tests that list a class's methods.

Java
for (Method m : Point.class.getDeclaredMethods()) {
if (m.getName().equals("clone")) {
System.out.println(m.getReturnType().getSimpleName() + " bridge=" + m.isBridge());
}
}
// Point bridge=false
// Object bridge=true
// (order of getDeclaredMethods is not guaranteed)

Frameworks that scan methods by name, such as older serialization or mapping libraries, usually filter out bridge methods with isBridge(). When we write such code ourselves, we should too, or we will process the same logical method twice.

5What Covariance Does Not Cover

Covariance applies only to return types, and only to reference types. Parameters cannot be narrowed in an override. If we declare a method in the subclass with a more specific parameter type, it is a new overload, not an override, and @Override will report a compile error. The reason is safety: a caller holding a parent reference may pass any argument the parent accepts, and the subclass must accept it too.

Primitive return types cannot vary at all. A parent method returning long cannot be overridden by one returning int, even though int converts to long, because covariance is about subtyping between classes, not about numeric conversions.

Java
class Shape { Shape copy() { return new Shape(); } boolean same(Shape s) { return true; } }
class Circle extends Shape {
@Override Circle copy() { return new Circle(); } // fine: covariant return
// @Override boolean same(Circle c) { ... } // compile error: does not override
boolean same(Circle c) { return false; } // compiles, but is an overload
}
Shape s = new Circle();
System.out.println(s.same(new Circle())); // true: Shape.same(Shape) is called

The last line is the classic trap. The static type of s is Shape, so the compiler picks same(Shape), which Circle does not override. Overloads are chosen at compile time from declared types, while overrides are chosen at run time from the object.

6Follow-up questions

Do generics use the same bridge mechanism?

Yes. When a class implements Comparable<Point> with compareTo(Point), erasure means the interface method is compareTo(Object). javac generates a bridge compareTo(Object) that casts the argument to Point and calls the real method. That cast is why a wrong argument type passed through a raw type fails with ClassCastException inside the bridge.

Can an interface method be overridden covariantly?

Yes. An implementing class or a sub-interface may declare a more specific return type, for example an interface method returning Collection<String> can be implemented by a method returning List<String>. The same bridge method generation applies.

Is clone() a good way to copy objects in modern Java?

Usually not. clone() has awkward rules: it is shallow, it bypasses constructors, and it fights with final fields. A copy constructor, a static factory such as copyOf, or simply an immutable record that never needs copying is clearer. Covariant returns are still useful elsewhere, especially in builders and factory methods.

7Conclusion

In this answer, we saw that a covariant return type lets an override return a subtype of the original return type, so Point.clone() can return Point and callers need no cast. Because the JVM includes the return type in a method's descriptor, javac adds a synthetic bridge method with the original Object clone() signature that forwards to the covariant one, which keeps older callers linking. We also saw that covariance applies only to reference return types, while a narrowed parameter becomes an overload rather than an override.

What to say in the interviewAbout 80 seconds aloud

Open with

A covariant return type means an overriding method can declare a more specific return type than the method it overrides, as long as it's a subtype. The usual example is clone.

Then, if they want more

Object.clone is protected and returns Object, so in my Point class I override it as public Point clone, call super.clone inside a try block, cast once, and turn the impossible CloneNotSupportedException into an AssertionError. Callers then write Point b = a.clone() with no cast. What's interesting is what javac does.

The JVM identifies methods by name plus full descriptor, including the return type, so at the bytecode level Point clone and Object clone are different methods. To keep overriding working, javac generates a synthetic bridge method in Point with the original signature, Object clone, whose body just calls the Point version.

Code compiled against Object.clone lands on the bridge and still gets my behaviour. You can see it with javap or with Method.isBridge in reflection. The same mechanism is used for generics, for example compareTo with a typed parameter. Finally, covariance only applies to reference return types.

You can't narrow a parameter; that creates an overload, not an override, and @Override catches the mistake. And primitive return types can't change at all.

About this answer

Last updated
12 November 2026
Versions it names
Java 5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a covariant return type? Show an example with clone() and explain what the...": https://scrivio-interview-library.netlify.app/java/oop-principles/38b85435a6/