java保护方法可访问性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3058976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java protected method accessibility
提问by JavaUser
In the below code the Consumer class can access the protected method of Parent class.How is it possible since there is no relation between Parent and Consumer class.Please explain
在下面的代码中,消费者类可以访问父类的受保护方法。父类和消费者类之间没有关系,这怎么可能。请解释
class Parent {
public void method1(){
System.out.println("PUBLIC METHOD");
}
private void method2(){
System.out.println("PRIVATE METHOD");
}
protected void method3(){
System.out.println("PROTECTED METHOD");
}
}
public class Consumer {
public static void main(String[] args){
Parent parentObj = new Parent();
parentObj.method1();
//parentObj.method2();
parentObj.method3();
}
}
Thanks
谢谢
采纳答案by atamanroman
protected
means: same package or by inheritance. Since your classes are both in the default package
(not recommended in real life), protected
enables access. By the way: if you tried to test java access control, you forgot default access
(default access
= no modifier = package private
).
protected
表示:相同的包或通过继承。由于您的课程都在default package
(现实生活中不推荐)中,因此protected
启用访问。顺便说一句:如果您尝试测试 java 访问控制,您忘记了default access
( default access
= no modifier = package private
)。
private
access on the other hand means: access from nowhere except this particular class (and non-static inner classes, which are still member of the host-class).
private
另一方面,访问意味着:除了这个特定的类(和非静态内部类,它们仍然是宿主类的成员)之外,从任何地方都可以访问。
回答by Adam Crume
First of all, they're in the same package. Secondly, Consumer is an inner class of Parent.
首先,它们在同一个包中。其次,Consumer 是 Parent 的内部类。
回答by Rakesh Goyal
Adam you are right! They are in the same package thats why consumer class is able to access protected method of parent class. Consumer is not an inner class of parent.
亚当你是对的!它们在同一个包中,这就是为什么消费者类能够访问父类的受保护方法。消费者不是父类的内部类。
回答by Monis Iqbal
回答by polygenelubricants
Here are the relevant excerpts from the Java Language Specification:
以下是 Java 语言规范的相关摘录:
JLS 6.6 Access Control
The Java programming language provides mechanisms for access control, to prevent the users of a
package
orclass
from depending on unnecessary details of the implementation of thatpackage
orclass
. If access is permitted, then the accessed entity is said to be accessible.JLS 6.6.1 Determining Accessibility
- [...]
- A member/constructor of a reference type is accessible only if the type is accessible and the member/constructor is declared to permit access:
public
: access is permitted.protected
: access is permitted only when one of the following is true:
- Access to the member or constructor occurs from within the
package
containing the class in which theprotected
member or constructor is declared.- Access is correct as described in JLS 6.6.2 Details on
protected
Access.
- A
protected
member/constructor of an object may be accessed from outside thepackage
in which it is declared only by code that is responsible for the implementation of that object.private
: access is permitted if and only if it occurs within the body of the top level class that encloses the declaration of the member or constructor.- Otherwise, we say there is default access, which is permitted only when the access occurs from within the
package
in which the type is declared.
JLS 6.6 访问控制
Java编程语言提供了机制的访问控制,以防止一个用户
package
或class
从依赖于该实现的不必要的细节package
或class
。如果允许访问,则称被访问的实体是可访问的。JLS 6.6.1 确定可访问性
- [...]
- 仅当类型可访问且成员/构造函数声明为允许访问时,引用类型的成员/构造函数才可访问:
public
: 允许访问。protected
:仅当以下情况之一为真时才允许访问:
- 对成员或构造函数的访问发生在
package
包含声明该成员或构造函数的类中protected
。- 访问是正确的,如JLS 6.6.2
protected
访问详细信息中所述。
- 甲
protected
物体的构件/构造可以从外部的访问package
在其声明仅通过代码,负责该对象的执行。private
: 当且仅当它出现在包含成员或构造函数声明的顶级类的主体内时,才允许访问。- 否则,我们说有默认访问,只有当访问发生在
package
声明类型的内部时才被允许。
The section in bold is the answer to the question in this scenario: Parent
and Consumer
belong to the same package
, thus, at the very least, protected
members of Parent
are accessible from Consumer
.
粗体部分是此场景中问题的答案:Parent
和Consumer
属于相同的package
,因此,至少, 的protected
成员Parent
可以从 访问Consumer
。