Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › OOP II — Inheritance, Polymorphism, Interfaces

What are covariant return types, and what rules constrain an overriding method's signature, exceptions, and visibility?

Level III · MidOOP II — Inheritance, Polymorphism, Interfaces
Short answer

An override may return a subtype of the original return type (covariant returns), which is why a clone() override can return the concrete type.

Explanation

The override cannot reduce visibility (a public method can't become protected), cannot throw broader or new checked exceptions than the parent declared, and must keep the same parameter list or it becomes an overload instead. @Override is worth using because it makes the compiler catch a signature mismatch that would otherwise silently create an overload.

Common mistakes
  • Widening the thrown checked exceptions in an override.
  • Reducing visibility in an override (public to protected is not allowed).

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for OOP II — Inheritance, Polymorphism, Interfaces.

Practise free

More on OOP II — Inheritance, Polymorphism, Interfaces