java protected method accessibility
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
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
).
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).
Here are the relevant excerpts from the Java Language Specification:
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.
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
.
See also
- Java Tutorials/Controlling Access to Members of a Class
Due to package protection.
http://mindprod.com/jgloss/packagescope.html
First of all, they're in the same package. Secondly, Consumer is an inner class of Parent.
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.
精彩评论