lovubuntu / checker-framework

Automatically exported from code.google.com/p/checker-framework
0 stars 0 forks source link

Contravariance via subtyping in Java #330

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Read the "Required Skills" paragraph in the introduction. Link: 
https://code.google.com/p/checker-framework/wiki/Ideas#Introduction
2. Test the correctness of the statement about subtyping and contravariance by 
writing two Java test classes.

What is the expected output? What do you see instead?
I'd expect that subtyping would allow arguments to change contravariantly.
I see instead, that Java does not allow that. 

What version of the product are you using? On what operating system?
Java 8, Win 7 pro x86

Please provide any additional information below.

To test whether Java allows contravariance via subtyping, please copy/paste the 
relevant example of the following StackOverflow answer into your favorite IDE, 
and annotate Sub#doSomething(Object) with @Override:
https://stackoverflow.com/questions/2501023/demonstrate-covariance-and-contravar
iance-in-java#answer-2501513

This will give you a compiler error, indicating, that this methods overloads 
Super#doSomething(String) instead of overriding it. Thus, Java does not allow 
contravariance via subtyping.

This is also mentioned here:
https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)#C
ontravariant_method_argument_type

Is this a mistake in the Wiki or did I misunderstand the statement?

Thanks,
Matthias

Original issue reported on code.google.com by matthias...@gmail.com on 17 May 2014 at 9:50

GoogleCodeExporter commented 9 years ago
Thanks for your comment, Matthias!  The statement was written in a way that was 
prone to misinterpretation, and I apologize for that.  I have now corrected it, 
so the wiki page reads as follows (added text is surrounded by asterisks):

  For example, you should know that subtyping *theory* permits arguments to change contravariantly *(even though Java forbids it for reasons related to overloading)*, whereas return values may change covariantly *both in theory and in Java*.

Thanks for pointing this out so that we could clarify the meaning -- I 
appreciate it.

Original comment by michael.ernst@gmail.com on 25 May 2014 at 4:35