Which three statements are true? (Choose three.)
A. A final method in class X can be abstract if and only if X is abstract.
B. A protected method in class X can be overridden by any subclass of X.
C. A private static method can be called only within other static methods in class X.
D. A non-static public final method in class X can be overridden in any subclass of X.
E. A public static method in class X can be called by a subclass of X without explicitly referencing the class X.
F. A method with the same signature as a private final method in class X can be implemented in a subclass of X.
G. A protected method in class X can be overridden by a subclass of X only if the subclass is in the same package as X.
这个问题来自 SCJP。答案是 BEF
我理解 BE,但 F 说该方法是“私有(private)最终”,那么子类如何覆盖该方法,因为它定义为最终和私有(private)方法。
请清除F点。
最佳答案
答案 F 不是覆盖的示例,而是方法范围的示例。在类中声明为 private
的方法对该类的子类不可见,因此不存在重叠;子类在父类(super class)中看不到它可以重写的方法。子类有自己的方法 void foo(int)
,完全不知道父类(super class)的 private void foo(int)
。
关于java - 父类(super class)中的私有(private)final可以被覆盖吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21922663/