my dog learned polymorphism
The moose likes Beginning Java and the fly likes Covariant Return Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Covariant Return" Watch "Covariant Return" New topic

Covariant Return

Krep Lock
Ranch Hand

Joined: Sep 19, 2006
Posts: 43
Some of the nuances of polymorphism remain foggy to me. Today it's covariant return types. I read a description of this feature and was surprised there is even a name for it - but apparently this behavior was illegal in previous versions. Or, maybe I'm missing something.

My understanding: covariant return types mean you can change the return type of an overridden method, as long as the new return type is derivative of the original return type. Is that it, or am I missing something about this feature? Here's my example:

[ February 03, 2008: Message edited by: Krep Lock ]
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
No, you aren't missing anything. And yes, it was introduced with Java 5.

Covariant return types are possible because they don't violate Liskov's Substitution Principle.

Interestingly, the JVM always supported them, it was only the language specification (and the compiler, consequently) that needed to be changed for this feature.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Covariant Return
It's not a secret anymore!