为什么此代码调用私有方法?

时间:2019-10-16 21:58:23

标签: java

有人可以向我解释为什么以下代码的输出是“ From app class”。我不能把头缠住它。

public abstract class App
{
    private void run()
    {
        System.out.println("From app class");
    }

    public static void main(String[] args) {
        App object = new OtherClass();
        object.run();
    }
}

class OtherClass extends App
{
    protected void run()
    {
        System.out.println("From otherclass");
    }
}

结果:来自应用程序类

为什么当我更改为这一行代码时,输​​出为“ From otherclass”:

public static void main(String[] args) {
      OtherClass object = new OtherClass();
      object.run();
}

0 个答案:

没有答案
相关问题