Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are contravariant parameter types in Java not allowed for overriding?

Tags:

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?

like image 322
Will Avatar asked Sep 15 '12 17:09

Will


1 Answers

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).

like image 109
pickypg Avatar answered Jan 03 '23 02:01

pickypg