contravariance : Java Glossary

go to home page C words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
contravariance
In Java, when you override a method, both parameters and return type must match exactly. In other languages, such as Eiffel, under certain circumstances, they need not precisely match. Here is a definition from Greg Weeks weeks@orpheus.dtc.hp.com Suppose I have the following interface:
interface Dog_printer {
void print(Dog d);
}

Suppose further that a class Animal_printer implements the method:

void print(Animal a);

Can I say that Animal_printer implements Dog_printer? Logically, I should be able to. Anywhere that a Dog_printer is expected, I can supply an Animal_printer, and the dog will get printed. (Because all dogs are animals.) So, logically, there is the relation:

Animal_printer is a subtype of Dog_printer.

Although logically correct, this is weird looking. Then again, something that can print any animal "extends" something that can print any dog, so maybe it isn’t so weird. Anyway, to determine whether Animal_printer is a subtype of Dog_printer, we have used the "contravariant argument rule". Similarly, there is a "covariant return value rule" that would state that:

Dog_creator is a subtype of Animal_creator.


CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.61] Spread the Net
You are visitor number 7,489.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/contravariance.html J:\mindprod\jgloss\contravariance.html