In Java, is it possible to override methods if return types are respectively a primitive and its wrapper class?

后端 未结 4 473
独厮守ぢ
独厮守ぢ 2021-01-07 10:27

While working with the idea of overriding and overridden methods in Java I have noticed that there is some flexiblity given for the return types of such methods.

Her

4条回答
  •  伪装坚强ぢ
    2021-01-07 10:52

    You can only return same type or a sub-class of the parent's return type and its known as Co-Variant return types.

    The compiler lets you auto-box and unbox between primitives and wrappers but this doesn't make one a sub-class of the other. Primitives are not classes and cannot be used in the way you have.

提交回复
热议问题