Java语言规范的第8.1.5节专门允许这种情况:
类中的单个方法声明可以实现多个超级接口的方法。例如,在代码中:
interface Fish { int getNumberOfScales(); }interface Piano { int getNumberOfScales(); }class Tuna implements Fish, Piano { // You can tune a piano, but can you tuna fish? int getNumberOfScales() { return 91; }}getNumberOfScales类中的方法Tuna的名称,签名和返回类型与interface中声明的方法匹配,Fish并且还与interface中声明的方法匹配Piano;它被认为同时实现。
然后,文本继续指出,如果方法签名具有不同的返回类型(例如
double和)
int,则将无法在同一类中实现两个接口,并且会产生编译时错误。



