回答一:这个java的规定。回答二:无论TestA和TestB是否在一个包里面,TestB是TestA的子类。TestA.a()如果modifier为private,则TestA.a()对其子类是不可见的,TestB中的a()是一个new method,即不是override。这种情况下,new TestB()->TestA()->a(),a()没有被override,直接使用基类的版本。

解决方案 »

  1.   

    问题一:类B继承类A,并且覆盖类A中的方法a,为什么类B中的a方法的访问控制属性一定得大于等于类A中的a方法的访问控制属性。
    这是肯定需要大于的啊,在编译的时间B 继承A,也是A的属性先编译,而B的属性后编译的啊。我们可以定义 A instance = new B();也就是这个道理啊,后面的B需要有比A更大的访问控制属性,才能确保A中有的B是一定可以实现啊。问题二:
    1、TestA和TestB在同一包中
    如果TestA中的的a方法的访问控制属性为private,程序结果为:TestA.在这个时间new TestB();的时间,TestB()没有构件器,调用父类的构建器new TestA(),而new TestA()的时间调用private方法a,输出TestA。如果TestA中的的a方法的访问控制属性为pubilc、protected、无,程序结果为:TestB一样new TestB();的时间,调用父类的构建器new TestA(),而new TestA()的时间调用重写的方法a,输出TestB。在同一个包中,default的方法是可以继承的,也就是说这个时间a()方法是重写。而不在同一个包中,default的方法对于其他的包是private的
      

  2.   

    原因如下:
    private :不能继承
    public :继承
    protected:包内继承,包外private 
    你的,明白?
      

  3.   

    chenyuan_tongji(codeguru高手的回答,非常漂亮
    当TestA中的a()被声明为private后,其子类是不集成超类的private的元素的,所以就像chenyuan_tongji说的,再TestB中a()是新的方法,不是重载TestA中的a()
    情况二一样,无对包外的方法或类来说就是private,同解了。
      

  4.   

    read the following carefully :Java uses three explicit keywords to set the boundaries in a class: public, private, and protected. Their use and meaning are quite straightforward. These access specifiers determine who can use the definitions that follow. public means the following definitions are available to everyone. The private keyword, on the other hand, means that no one can access those definitions except you, the creator of the type, inside member functions of that type. private is a brick wall between you and the client programmer. If someone tries to access a private member, they’ll get a compile-time error. protected acts like private, with the exception that an inheriting class has access to protected members, but not private members. Inheritance will be introduced shortly.
      

  5.   

    instantiate B时会先instantiate A,即生成一个A的object,于是执行TestA(),而在执行TestA()中的a()时,会首先在class A中找a(),如果没有,再从class B中找a(),无论a()是否被overridden