首页 > 代码库 > java-Cannot reduce the visibility of the inherited method from 父类

java-Cannot reduce the visibility of the inherited method from 父类

在Java里面,当A类继承B类,在B类里面重写(或叫覆写/override)A类的方法时,有一个规定,那就是:子类的该方法的权限修饰符范围应该是大于等于父类。

  class A{

    protected method() { }

  }

  class B extends A{

    private method(){ }

    //private是错误的,会出现错误提示:Cannot reduce the visibility of the inherited method from B

    //将protected,public或者缺省(不加)都是可以正常编译。

  }