Superclass方法与Interface默认方法冲突解决
Super class method and Interface default method conflict resolution
考虑下面的例子,
public class Testing extends SupCls implements Intf {
public static void main(String[] args) {
new Testing().test();
}
}
class SupCls {
public void test() {
System.out.println("From SupCls");
}
}
interface Intf {
public default void test() {
System.out.println("From Intf");
}
}
如您所见,SupCls
class 和 Intf
接口之间没有连接。但是两者都是定义一个通用的方法
并且Testing
class 正在扩展SupCls
并实施Intf
。
所以,当我在 Testing
上调用 test()
方法时,输出是
From SupCls
我认为这是有道理的,因为从 class 扩展应该比从接口实现更高的优先级。
但 eclipse 报告情况并非如此,如下面的屏幕截图所示。
我坚信这是 Eclipse中的一个错误。
但在假设之前,这种行为是否定义并记录在 JLS 中?或者是否有其他定义此行为的内容?
编辑:如果重要的话,Eclipse 版本是 Mars Release (4.5.0)。
看来 eclipse 版本很重要!
在 Eclipse Luna (4.4.1) 中,引用指向 SupCls
而不是 Intf
可能是 Eclipse Mars 中的一个错误
这当然是 eclipse 中的错误,但在代码完成建议中而不是在编译器中。将鼠标悬停在测试调用或 Open Declaration 上会将您带到 SupCls 方法,运行 代码正确打印 "From SupCls",这证明了这一点。请针对 jdt ui 提交错误以供调查
你的假设是正确的,继承自superclass的具体方法优先于继承自interface
的default
方法:
JLS §8.4.8. Inheritance, Overriding, and Hiding
A class C
inherits from its direct superclass and direct superinterfaces all abstract
and default (§9.4) methods m
for which all of the following are true:
…
- No method declared in
C
has a signature that is a subsignature (§8.4.2) of the signature of m
.
- No concrete method inherited by
C
from its direct superclass has a signature that is a subsignature of the signature of m
.
引用的第二个项目符号适用于此,有一个从直接 superclass 继承的具有适当签名的具体方法,因此不继承 default
方法。
文档甚至通过附加说明消除了任何疑问:
Note that it is possible for an inherited concrete method to prevent the inheritance of an abstract or default method. (Later we will assert that the concrete method overrides the abstract or default method "from C".)
所以当谈到 class Testing
.
时,就像 SupCls.test()
覆盖 Intf.test()
换句话说,你是对的,这是 Eclipse 中的一个错误,但只要它只影响提案的呈现方式,我就认为它是 次要的漏洞。插入的源将是相同的,无论 D 是否已在提案中呈现。
考虑下面的例子,
public class Testing extends SupCls implements Intf {
public static void main(String[] args) {
new Testing().test();
}
}
class SupCls {
public void test() {
System.out.println("From SupCls");
}
}
interface Intf {
public default void test() {
System.out.println("From Intf");
}
}
如您所见,SupCls
class 和 Intf
接口之间没有连接。但是两者都是定义一个通用的方法
并且Testing
class 正在扩展SupCls
并实施Intf
。
所以,当我在 Testing
上调用 test()
方法时,输出是
From SupCls
我认为这是有道理的,因为从 class 扩展应该比从接口实现更高的优先级。
但 eclipse 报告情况并非如此,如下面的屏幕截图所示。
我坚信这是 Eclipse中的一个错误。
但在假设之前,这种行为是否定义并记录在 JLS 中?或者是否有其他定义此行为的内容?
编辑:如果重要的话,Eclipse 版本是 Mars Release (4.5.0)。
看来 eclipse 版本很重要!
在 Eclipse Luna (4.4.1) 中,引用指向 SupCls
而不是 Intf
可能是 Eclipse Mars 中的一个错误
这当然是 eclipse 中的错误,但在代码完成建议中而不是在编译器中。将鼠标悬停在测试调用或 Open Declaration 上会将您带到 SupCls 方法,运行 代码正确打印 "From SupCls",这证明了这一点。请针对 jdt ui 提交错误以供调查
你的假设是正确的,继承自superclass的具体方法优先于继承自interface
的default
方法:
JLS §8.4.8. Inheritance, Overriding, and Hiding
A class
C
inherits from its direct superclass and direct superinterfaces allabstract
and default (§9.4) methodsm
for which all of the following are true:…
- No method declared in
C
has a signature that is a subsignature (§8.4.2) of the signature ofm
.- No concrete method inherited by
C
from its direct superclass has a signature that is a subsignature of the signature ofm
.
引用的第二个项目符号适用于此,有一个从直接 superclass 继承的具有适当签名的具体方法,因此不继承 default
方法。
文档甚至通过附加说明消除了任何疑问:
Note that it is possible for an inherited concrete method to prevent the inheritance of an abstract or default method. (Later we will assert that the concrete method overrides the abstract or default method "from C".)
所以当谈到 class Testing
.
SupCls.test()
覆盖 Intf.test()
换句话说,你是对的,这是 Eclipse 中的一个错误,但只要它只影响提案的呈现方式,我就认为它是 次要的漏洞。插入的源将是相同的,无论 D 是否已在提案中呈现。