Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Covariant Return

 
Krep Lock
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic