Java子类可以继承父类的所有属性吗

子类可以继承父类的所有属性吗

答案:

不可以,当然是不可以全部属性和方法都继承,那么哪些不可以继承?

最起码私有的就不可以,私有的属性和方法都不可以,其他的那就需要继续去测试了。

我用的是jdk1.8来测试的。

测试代码:

/**
 * @author xin麒
 * @date 2023/7/24 18:25
 */
public class Test02 {
    public static void main(String[] args) {
        Son son = new Son();
        System.out.println("son.footCount is "+son.footCount);
        System.out.println("son.handsCount is "+son.handsCount);
        System.out.println("son.headCount is "+son.headCount);
        son.print();
        son.say();
    }
}

class Father{
    public int handsCount = 2;
    private int headCount = 1;
    int footCount = 2;

    public void print(){
        System.out.println("this is print method in Father");
    }
    private void say(){
        System.out.println("say in father`s method");
    }

}
class Son extends Father{
}

验证1-运行后报错:

D:\start_java\JavaProject_\project2023-06\theadLearning\june24-01\src\test\java\com\xinqi\july24\Test02.java:12:51
java: headCount 在 com.xinqi.july24.Father 中是 private 访问控制

验证2-或者说直接看idea的提示吧,都直接警告了:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值