Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can only access protected members of a type in a different package if the compile-time type of the expression you're referencing it through is either your own class or a subclass. (Where "your" class is the class containing the code.) Your own class has to be a subclass of the type which originally declares the method, too.</p> <p>Here's an example; assume that <code>Base</code> is in a different package to all the other classes:</p> <pre><code>package first; public class Base { protected void Foo() {} } // Yes, each class is really in its own file normally - but treat // all the classes below as being in package "second" package second; public class Child extends Base { public void OtherMethod(Object x) { ((Base) x).Foo(); // Invalid: Base is not Child or subclass ((Child) x).Foo(); // Valid: Child is Child ((GrandChild) x).Foo(); // Valid: GrandChild is subclass of Child ((OtherChild) x).Foo(); // Invalid: OtherChild is not Child or subclass } } public class GrandChild extends Child {} public class OtherChild extends Base {} </code></pre> <p>In other words, it's letting you have access to the protected members of "objects which are a like you".</p> <p>Details are in <a href="http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2" rel="noreferrer">section 6.6.2 of the Java Language Specification</a>:</p> <blockquote> <p>A <code>protected</code> 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.</p> <p><strong>6.6.2.1 Access to a protected Member</strong></p> <p>Let <em>C</em> be the class in which a protected member <em>m</em> is declared. Access is permitted only within the body of a subclass <em>S</em> of <em>C</em>. In addition, if Id denotes an instance field or instance method, then: If the access is by a qualified name <em>Q.Id</em>, where <em>Q</em> is an <em>ExpressionName</em>, then the access is permitted if and only if the type of the expression <em>Q</em> is <em>S</em> or a subclass of <em>S</em>. If the access is by a field access expression <em>E.Id</em>, where <em>E</em> is a <em>Primary</em> expression, or by a method invocation expression <em>E.Id(. . .)</em>, where <em>E</em> is a <em>Primary</em> expression, then the access is permitted if and only if the type of <em>E</em> is <em>S</em> or a subclass of <em>S</em>.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload