title: Java access to protected member in subclass in different package, using object reference of parent type
body: <p>I have the following code in two separate files.</p>
  
  <pre><code>package animal;
  
  public class Frog
  {
      protected void ribbit()
      {
          System.out.println("In Frog class!");
      }
  }
  
  
  
  
  
  package other;
  
  import animal.*;
  
  public class Tadpole extends Frog
  
  {
      protected void ribbit()
      {
          System.out.println("In Tadpole class!");
      }
  
      public static void main(String[] args)
      {
          Tadpole t = new Tadpole();
          t.ribbit();
  
          Frog f = new Tadpole();
          f.ribbit(); // Does not compile
      }
  }
  </code></pre>
  
  <p>The first <code>Tadpole</code> object assigned to <code>Tadpole</code> type obviously compiles fine and the call to <code>ribbit()</code> will be to the <code>Tadpole</code>'s <code>ribbit()</code> implementation. The second <code>Tadpole</code> object that is created and assigned to a <code>Frog</code> reference. However, the call to <code>ribbit()</code> results in a compiler error. </p>
  
  <p>I know that if you create a subclass object in the subclass and assign to a superclass reference that is outside of the subclass's package and try to call a superclass method, this is not allowed. But in this case, shouldn't polymorphism make the object reference "f" call the <code>Tadpole</code>'s <code>ribbit()</code> method since a <code>Tadpole</code> object was assigned to it? Why does this cause a compiler error and why is this is not allowed?</p>