title: Java - declaring from Interface type instead of Class
body: <p>In my quest to correctly grasp Interface best practices, I have noticed declarations such as:</p>
  
  <pre><code>List&lt;String&gt; myList = new ArrayList&lt;String&gt;();
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>ArrayList&lt;String&gt; myList = new ArrayList&lt;String&gt;();
  </code></pre>
  
  <p>-To my understanding the reason is because it allows flexibility in case one day you do not want to implement an ArrayList but maybe another type of list.</p>
  
  <p>With this logic, I set up an example:</p>
  
  <pre><code>public class InterfaceTest {
  
      public static void main(String[] args) {
  
          PetInterface p = new Cat();
          p.talk();
  
      }
  
  }
  
  interface PetInterface {                
  
      public void talk();
  
  }
  
  class Dog implements PetInterface {
  
      @Override
      public void talk() {
          System.out.println("Bark!");
      }
  
  }
  
  class Cat implements PetInterface {
  
      @Override
      public void talk() {
          System.out.println("Meow!");
      }
  
      public void batheSelf() {
          System.out.println("Cat bathing");
      }
  
  }
  </code></pre>
  
  <p>My question is, I cannot access the batheSelf() method because it only exists for Cat. That leads me to believe that I should only declare from an Interface if I am only going to use methods declared in the Interface (and not extra methods from the subclass), otherwise I should declare from the Class directly (in this case Cat). Am I correct in this assumption?</p>