<分区>
我有这两个类,Dog
和 Beagle
扩展了 Dog
。
class Dog {
protected String bark() {return "woof "; }
}
class Beagle extends Dog {
private String bark() { return "arf "; }
}
class Test {
public static void main(String args[]) {
Dog[] dogs = {new Dog(), new Beagle()};
for(Dog d : dogs)
System.out.print(d.bark());
}
}
当我使用任何其他编辑器而不是 Eclipse 时,上面的代码甚至无法编译。我收到此错误:
Attempting to assign weaker access privileges to bark() method in Beagle class.
您还可以看到此行为 here .
如果我使用 Eclipse Indigo(版本:3.7.2),这段代码可以正常编译,输出为:woof woof
。
请让我明白哪一个是正确的,为什么?
提前致谢!