关于Java继承属性覆盖的问题(向上转型)

案例代码:

public class Person {
    private String privateName = "Person privateName...";
    protected String protectedName = "Person protectedName...";
    /*default*/ String name =  "Person defaultName";
    public String publicName = "Person publicName";

    public static String staticName = "Person staticName";
    public final String finalName = "Person finalName";

    public String getPrivateName() {
        return privateName;
    }
}
package abstractDemo;

public class Child extends Person {

    private String privateName = "Child privateName...";
    protected String protectedName = "Child protectedName...";
    /*default*/ String name =  "Child defaultName";
    public String publicName = "Child publicName";

    public static String staticName = "Child staticName";
    public final String finalName = "Child finalName";


//    @Override
//    public String getPrivateName() {
//        return privateName;
//    }

    public static void main(String[] args) {
        Person person1 = new Person();
        Person person2 = new Child();
        Child child = new Child();

        System.out.println("Person person1 = new Person();");
        System.out.println("publicName:"+person1.publicName);
        System.out.println("defaultName:"+person1.name);
        System.out.println("protectedName:"+person1.protectedName);
        System.out.println("privateName:"+person1.getPrivateName());
        System.out.println("staticName:"+ Person.staticName);
        System.out.println("finalName:"+person1.finalName);
        System.out.println("----------------------------");


        System.out.println("Person person2 = new Child();");
        System.out.println("publicName:"+person2.publicName);
        System.out.println("defaultName:"+person2.name);
        System.out.println("protectedName:"+person2.protectedName);
        System.out.println("privateName:"+person2.getPrivateName());
        System.out.println("finalName:"+person2.finalName);
        System.out.println("----------------------------");


        System.out.println("Person child = new Child();");
        System.out.println("publicName:"+child.publicName);
        System.out.println("defaultName:"+child.name);
        System.out.println("protectedName:"+child.protectedName);
        System.out.println("privateName:"+child.privateName);
        System.out.println("staticName:"+ Child.staticName);
        System.out.println("finalName:"+child.finalName);

    }

}

输出结果:
在这里插入图片描述
如果放开子类getPrivateName方法的注释
在这里插入图片描述

结论:

  • 声明父类的引用指向子类的实例就是向上转型,实例的属性值取决于实例声明的类型,不取决于创建实例的类型。也就是向上转型后,只能调用父类属性,不能调用子类属性。
  • 这里可以取到子类的privateName,是因为子类中重写了getPrivateName方法
  • 除了private类型,修饰符并没有影响属性的覆盖
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值