Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Benefits of covariant return types?

 
Kenny Johnson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the benefits of covariant return types? Also, does anyone know a simple definition of the word covariant. I went to a few dictionary sites but the definitions were so math orientated that I got confused...
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A covariant return is when you override a method and make the return type of the method in the subclass different than the return type of the method in the superclass. The return type of the subclass' method must be a subclass of the return type of the superclass' method. In other words you can make the return type of the subclass' method the same or less general.

This wasn't allowed before Java 5.

Confused? Good. Here's an example:



Apple.getFoodSubstance() overrides the method Fruit.getFoodSubstance() and has a different return type -- a covariant return type. Apple.getFoodSubstance() returns an Apple which is a subclass of Fruit so the restriction is satisfied.
 
Kenny Johnson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the nice example. What does the word covariant mean though? Just wondering. The definitions on other sites are all very Math orientated.
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Merriam-Webster says:

covariant: varying with something else so as to preserve certain mathematical interrelations

In the case of Java covariants that could be:

covariant: varying with something else so as to preserve certain method interrelations

In other words, changing the return type while maintaining the rest of the method signature.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Johnson:

In other words, changing the return type while maintaining the rest of the method signature.


Not quite correct. It's the return type that is covariant, not the method itself. (Some languages also allow covariant argument types).

It simply means that the return type of an operation in a subtype may be more specific than the return type of the inherited operation.
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite correct.


I stand corrected. Here's what Wikipedia has to say:

http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29
[ January 02, 2007: Message edited by: Scott Johnson ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic