Interface (two methods, different return types depending on class) Java

前端 未结 4 1192
甜味超标
甜味超标 2021-01-18 17:49

Any class that extends an interface must implement the methods declared in the interface. Not sure if this is possible but what I want to do is the following :



        
4条回答
  •  醉话见心
    2021-01-18 18:30

    No, it's not possible. The interface serves as a "binding contact" of the signatures that are available.

    If you want you can have the function return an Object and by that allow different implementations to return values of different types, but:

    1. It must be references, not primitives.
    2. You loose type safety at the call site.

提交回复
热议问题