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?
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With