private access modifiers in Java?

This question already has an answer here:

  • In Java, difference between package private, public, protected, and private 26 answers

  • The first answer is basically correct - protected members can be accessed by

  • classes from the same package
  • subclasses of the declaring class from other packages
  • However, there is a little trick:

    6.6.2 Details on protected Access

    A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

    It means that subclass from other package cannot access protected members of arbitrary instances of their superclasses, they can only access them on instances of their own type (where type is a compile-time type of expression, since it's a compile-time check).

    For example (assuming that this code is in Cat ):

    Dog dog = new Dog();
    Animal cat = new Cat();
    
    dog.testInstanceMethod(); // Not allowed, because Cat should not be able to access protected members of Dog
    cat.testInstanceMethod(); // Not allowed, because compiler doesn't know that runtime type of cat is Cat
    
    ((Cat) cat).testInstanceMethod(); // Allowed
    

    It makes sense, because accessing of protected members of Dog by Cat may break invariants of Dog , whereas Cat can access its own protected members safely, because it knows how to ensure its own invariants.

    Detailed rules:

    6.6.2.1 Access to a protected Member

    Let C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass S of C. In addition, if Id denotes an instance field or instance method, then:

  • If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S.
  • If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.
  • 6.6.2.2 Qualified Access to a protected Constructor

    Let C be the class in which a protected constructor is declared and let S be the innermost class in whose declaration the use of the protected constructor occurs. Then:

  • If the access is by a superclass constructor invocation super(. . .) or by a qualified superclass constructor invocation of the form E.super(. . .), where E is a Primary expression, then the access is permitted.
  • If the access is by an anonymous class instance creation expression of the form new C(. . .){...} or by a qualified class instance creation expression of the form E.new C(. . .){...}, where E is a Primary expression, then the access is permitted.
  • Otherwise, if the access is by a simple class instance creation expression of the form new C(. . .) or by a qualified class instance creation expression of the form E.new C(. . .), where E is a Primary expression, then the access is not permitted. A protected constructor can be accessed by a class instance creation expression (that does not declare an anonymous class) only from within the package in which it is defined.
  • See also:

  • Java Language Specification

  • In protected access the members are accessed in the same package and for inherited class member in another package can also be accessed.

    In package access the members of the classes in the same package can be accessed. Class members in other packages can't be accessed in package access.


    You have created an Cat instance and cast it to its super class type ie Animal type. As per Animal type its testInstanceMethod is visible in same package or any subtypes. If you didn't cast to Animal type the code will compile.

    Hope that helps

    ./Arun

    链接地址: http://www.djcxy.com/p/4120.html

    上一篇: 何时在shell变量中引用引号?

    下一篇: Java中的私人访问修饰符?