Monday, July 21, 2008

Java covariant return types

In my attempt to try to catch up with more recent features in Java and Linux, I found out that a feature introduced in Java 5 that I was not aware of is covariant return types, what is neat. You can override a method and change the return type, what was impossible before Java 5, as long the returned type is a subclass of the return type of the superclass method.

For example,
class SuperClass {
SuperClass getX() {
return new SuperClass();
class SubClass extends SuperClass {
SubClass getX() {
return new SubClass();

There is an interesting page here about it.
Post a Comment