title: Accessing Protected Variable Of An Instance Of A Class That Extends An Abstract Base Class?
body: <p>I have an abstract class called MyAction which contains a protected enum variable. The class is defined as follows:</p>
  
  <pre><code>package mypackage;
  
  public abstract class MyAction {
      public enum ActionId {
          ACTION1, ACTION2;
      }
  
      protected ActionId actionId;
  
      // constructor
      public MyAction(ActionId actionId) {
          this.actionId = actionId;
      }
  
      public ActionId getActionId() {
          return actionId;
      }    
      ...
      ...
  }
  </code></pre>
  
  <p>I created a specific action, MyAction1, that extends MyAction:</p>
  
  <pre><code>package mypackage;
  
  public class MyAction1 extends MyAction {
      public MyAction1() {
          super(ActionId.ACTION1);
      }
      ...
      ...
  }    
  </code></pre>
  
  <p>I have a singleton utility class (in the same package) that creates an instance of MyAction1 and stores it in a HashMap:</p>
  
  <pre><code>package mypackage;
  
  public class MyActionFactory {
      private static MyActionFactory theInstance;
      private HashMap&lt;ActionId, MyAction&gt; actions;
  
      private MyActionFactory() {
          actions = new HashMap&lt;ActionId, MyAction&gt;();
          MyAction1 myAction1 = new MyAction1();
          actions.put(myAction1.actionId, myAction1); // able to access protected variable actionId
      }
  
      public static VsActionFactory getInstance() {
          if (theInstance == null)
              theInstance = new VsActionFactory();
          return theInstance;
      }    
      ...
      ...
  }
  </code></pre>
  
  <p>Note that in the method <em>actions.put(<strong>myAction1.actionId</strong>, myAction1)</em> I am able to access the protected member <em>actionId</em>.</p>
  
  <p>Why is it that I can access the protected member <em>actionId</em> (contained in the base class <em>MyAction</em>) of the instance of <em>MyAction1</em>? I thought protected members were only accessible to subclasses. </p>
  
  <p>Does it have anything to do with <em>MyActionFactory</em> being in the same package as the others?</p>