There's no covariance between primitives. No primitive type is a sub type of any other. So you can't do this
class Parent {
public int method() {
return 0;
}
}
class Child extends Parent {
public short method() { // compilation error
return 0;
}
}
For the same reason, corresponding array types for int
and short
also are not covariant.
With array types, it's similar to your Number
example
class Parent {
public Number[] method() {
return null;
}
}
class Child extends Parent {
public Integer[] method() {
return null;
}
}
Similarly for Collection
types
class Parent {
public Collection<String> method() {
return null;
}
}
class Child extends Parent {
public List<String> method() {
return null;
}
}
Note the generic type argument has to be compatible (no covariance in generics, except in bounded wildcards).