java为什么说静态方法不能被继承?
我能够运行以下代码:
class A
{
public static void display()
{
System.out.println("Inside static method of superclass");
}
}
class B extends A
{
public void show()
{
display();
}
}
public class staticMethodInheritance {
public static void main(String[] args) {
B b = new B();
b.display();
}
}
现在我可以从类B的实例访问display()方法,那么为什么说静态方法不能被继承呢。如果我在类B中声明了一个方法display,那么可以说超类中的方法是隐藏的,子类中的方法是调用的,那么这不是重写方法时需要的行为吗
# 1 楼答案
与
inherited static (class) methods and inherited non-static (instance)
方法的唯一区别在于,当您编写具有相同签名的新静态方法时,the old static method is just hidden, not overridden.
静态方法是继承的,但不能被重写。它们可以被重新定义