看来我的理解错了,从官方资料上了解到(PS:请忽略该文章):
Private Members in a Superclass
A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
一、关于Java中访问修饰符的知识
注:打勾,表示可见,即在该位置上能够使用某个类的对象来访问(即使用)该类中特定访问修饰符修饰的属性或方法。也就是说,能看见就能访问。
使用类中的属性或方法,是通过类的对象来使用的。(排除类中的static和final修饰的属性和方法)而对于在本类中直接使用属性或方法,都是隐含着this这个关键字来使用这些属性和方法的,this就是本类对象的引用。
对于protected修饰符,应该注意一点,该关键字所修饰的属性和方法在不同包的子类中可见,这是指子类继承了父类中使用protected修饰的属性和方法。而不是指能够使用该父类的对象来访问父类中特定访问修饰符修饰的属性或方法。即使在不同包的子类中定义了父类的对象,访问该父类对象的protected修饰的属性和方法是不能的。因为不可见。
以可见度来理解访问修饰符的作用是比较好理解的。
二、关于Java中继承的知识
在Java中,所有的继承都是公有继承。即父类中的所有属性和方法,不管是用什么访问修饰符修饰,都被子类继承了。这就是公有继承含义所在吧。而对于覆写(重写,override)来说,只要在子类中,父类的属性或方法可见,则子类就可以选择覆写父类中的属性或方法。(属性被覆写的话,可能体现在不同子类中的属性初始值应该不一样的情况下吧)
注意:但是父类中的私有方法是不能被子类覆写的。
三、举例说明
准备工作(这里,setter、getter和构造方法就不予以给出源码):
1)com.here包中Person类的定义
[java] view plain copy
print?
- package com.here;
-
- public class Person {
- public int id = 0;
- protected String name = "defaultName";
- int age = 100;
- private String gender = "defaultGender";
-
- public void publicMethod() {
- System.out.println(this + " public method in superclass.");
- }
-
- protected void protectedMethod() {
- System.out.println(this + " protected method in superclass.");
- }
-
- void defaultMethod() {
- System.out.println(this + " default method in superclass.");
- }
-
- private void privateMethod() {
- System.out.println(this + " private method in superclass.");
- }
- }
2)com.there包中Student类的定义[java] view plain copy
print?
- package com.there;
-
- import com.here.Person;
-
- public class Student extends Person {
- @Override
- public void publicMethod() {
- System.out.println(this + " override public method in subclass.");
- }
-
- @Override
- protected void protectedMethod() {
- System.out.println(this + " override protected method in subclass.");
- }
- }
注:子类中并没有定义任何的成员属性,对于成员属性的覆写,这里不做说明。而对于父类中的public和protected修饰的方法在包外的子类中是可见的(protected修饰的方法之所以可见,是因为被子类继承了),故可以覆写这些方法。
3)在Person类中的main方法里,定义一个匿名内部类
[java] view plain copy
print?
- public static void main(String[] args) {
- Person anonymousInnerClassObject = new Person() {
- @Override
- public void publicMethod() {
- System.out.println(this + " override public method in anonymous inner class.");
- }
-
- @Override
- protected void protectedMethod() {
- System.out.println(this + " override protected method in anonymous inner class.");
- }
-
- @Override
- void defaultMethod() {
- System.out.println(this + " override default method in anonymous inner class.");
- }
- };
- }
注:对于该匿名内部类,因为与父类同在一个包中,故default修饰的方法在该子类中是可见的,所以可以覆写该方法。1、在Person里的main方法里,实例化一个Student类对象,访问继承下来的属性和方法
注:为了不受访问修饰符的影响,使用Person类型的对象引用来引用Student类型的对象。
[java] view plain copy
print?
- System.out.println("The information of a Student instance.");
- Person student = new Student(1, "wujilin", "male", 22);
- student.publicMethod();
- student.protectedMethod();
- student.defaultMethod();
- student.privateMethod();
- System.out.println(student.age);
- System.out.println(student.gender);
- System.out.println(student.id);
- System.out.println(student.name);
控制台输出:
The information of a Student instance.
com.there.Student@1fb8ee3 override public method in subclass.
com.there.Student@1fb8ee3 override protected method in subclass.
com.there.Student@1fb8ee3 default method in superclass.
com.there.Student@1fb8ee3 private method in superclass.
22
male
1
wujilin
若是使用Student类型的对象引用,则
错误提示均是:不可见
注:对于protectedMethod()方法不可见,是因为该方法在Student类的子类中才是可见的,而且Person类与Student类又不在同一个包中。而对于defaultMethod()和privateMethod()方法,在Student类中是不可见的。student这个对象引用又是Student类型的,也就是说在Student类中找不到这两个方法,也没办法到父类中寻找。
根据多态性的原理,父类类型的对象引用可以调用子类对象继承和覆写后的方法。
2、在Person类里的main方法里,实例化一个匿名内部类对象,访问继承下来的属性和方法
[java] view plain copy
print?
- System.out.println("The information of a anonymous inner class instance.");
-
- Person anonymousInnerClassObject = new Person() {
- @Override
- public void publicMethod() {
- System.out.println(this + " override public method in anonymous inner class.");
- }
-
- @Override
- protected void protectedMethod() {
- System.out.println(this + " override protected method in anonymous inner class.");
- }
-
- @Override
- void defaultMethod() {
- System.out.println(this + " override default method in anonymous inner class.");
- }
- };
-
- anonymousInnerClassObject.publicMethod();
- anonymousInnerClassObject.privateMethod();
- anonymousInnerClassObject.protectedMethod();
- anonymousInnerClassObject.defaultMethod();
-
- System.out.println(anonymousInnerClassObject.getId());
- System.out.println(anonymousInnerClassObject.name);
- System.out.println(anonymousInnerClassObject.getGender());
- System.out.println(anonymousInnerClassObject.age);
控制台输出:
The information of a anonymous inner class instance.
com.here.Person$1@61de33 override public method in anonymous inner class.
com.here.Person$1@61de33 private method in superclass.
com.here.Person$1@61de33 override protected method in anonymous inner class.
com.here.Person$1@61de33 override default method in anonymous inner class.
0
defaultName
defaultGender
100