Can overridden methods differ in return type?

前端 未结 12 1679
礼貌的吻别
礼貌的吻别 2020-11-22 16:11

Can overridden methods have different return types?

12条回答
  •  不知归路
    2020-11-22 16:51

    Yes it may differ but their are some limitations.

    Before Java 5.0, when you override a method, both parameters and return type must match exactly. In Java 5.0, it introduces a new facility called covariant return type. You can override a method with the same signature but returns a subclass of the object returned. In another words, a method in a subclass can return an object whose type is a subclass of the type returned by the method with the same signature in the superclass.

提交回复
热议问题