title: java - protected members accessed in derived class using base class instance
body: <p>I created instance of base class in derived class and tried to access protected members.  </p>
  
  <p>I can directly access protected members in a derived class without instantiating the base class. </p>
  
  <p>Base class:</p>
  
  <pre><code>package com.core;
  
  public class MyCollection {
  
        protected Integer intg;
  }
  </code></pre>
  
  <p>A derived class in the same package -</p>
  
  <pre><code>package com.core;
  
  public class MyCollection3 extends MyCollection { 
  
   public void test(){
  
    MyCollection mc = new MyCollection();
    mc.intg=1; // Works
   }
  }</code></pre>
  
  <p>A derived class in a different package -</p>
  
  <pre><code>package secondary;
  
  import com.core.MyCollection;
  
  public class MyCollection2 extends MyCollection{ 
  
   public void test(){
    MyCollection mc = new MyCollection();
    mc.intg = 1; //!!! compile time error - change visibility of "intg" to protected
   }
  }
  </code></pre>
  
  <p>How it is possible to access a protected member of a base class in a derived class using instance of base class when derived class is also in same package but not when derived class is in different package?</p>
  
  <p>If I mark protected member as "static" then I am able to access protected member of base class using instance of base class in a derived class which resides in a different package.</p>