Why is there no parameter contra-variance for overriding?
Posted
by Oak
on Stack Overflow
See other posts from Stack Overflow
or by Oak
Published on 2010-06-08T08:55:35Z
Indexed on
2010/06/08
9:02 UTC
Read the original article
Hit count: 252
C++ and Java support return-type covariance when overriding methods.
Neither, however, support contra-variance in parameter types - instead, it translates to overloading (Java) or hiding (C++).
Why is that? It seems to me that there is no harm in allowing that. I can find one reason for it in Java - since it has the "choose-the-most-specific-version" mechanism for overloading anyway - but can't think of any reason for C++.
Example (Java):
class A {
public void f(String s) {...}
}
class B extends A {
public void f(Object o) {...} // Why doesn't this override A.f?
}
© Stack Overflow or respective owner