Question

When overriding a method of a superclass, Java allows the return type to be covariant.

Why are contravariant parameter types in contrast not allowed when overriding methods?

Was it helpful?

Solution

Because that's called overloading.

In particular, the return type type can be covariant because it is not considered when overloading, and it therefore still matches the superclass or interface's implementation. Parameters are considered when overloading. You very well might have an optimization with Number doSomethingWithNumber(Integer value) compared to Number doSomethingWithNumber(Number value).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top