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.
这是肯定需要大于的啊,在编译的时间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的
private :不能继承
public :继承
protected:包内继承,包外private
你的,明白?
当TestA中的a()被声明为private后,其子类是不集成超类的private的元素的,所以就像chenyuan_tongji说的,再TestB中a()是新的方法,不是重载TestA中的a()
情况二一样,无对包外的方法或类来说就是private,同解了。