Covariant return types


o Prior to JDK 1.5, a subclass could not override a method using a different return type

o In JDK 1.5, a method can be overridden with a different return type as long as the new return type extends the original

o A return type that differs from that of the method it is overriding is called a “covariant return”

class Point3D extends Point2D …

class Position2D{

Point2D getPosition() …

}

// Overrides getPosition() method with different return type.

class Position3D extends Position2D {

Point3D getPosition() … //permissible since Point3D extends Point2D

}

About mayurmevada
I am a software professional with having helping nature...

One Response to Covariant return types

  1. Jigar says:

    This is really useful and informative information. Frankly saying…i was not aware about the difference between jdk version . and i did a lot of goggling for that but not getting proper answer but after reading to this blog my goggling for this article has over.

    Thanks mayur.

Leave a comment