java修饰符访问范围,Java访问修饰符的可访问范围

本文介绍了Java中的访问修饰符private、protected和public的访问范围。private成员只能在同一个类中访问,public成员在任何地方都可访问,而protected成员除在同一包内访问外,还可被不同包的子类访问。要从不同包的类中访问protected成员,你需要使你的类成为定义该成员的类的子类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java has private, protected, and public access modifiers. Can you explain the accessibility scope of these modifiers.

How can I access a protected member within a different package?

解决方案

For better understanding you need to see this

Access Modifiers

Same Class Same Package Subclass Other packages

public Y Y Y Y

protected Y Y Y N

no access modifier Y Y N N

private Y N N N

Here the important difference is between Default and protected.

Default: Never accessible outside the package

Protected: Only accessible outside the package, if and only if the class is a subclass

Please see this for further details.

Edit: As your question's answer is also the same that You can access the protected member by make your class a sub class of the class , in which protected member is defined

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值