title: Should variables always be declared with interface in Java?
body: <p>One often sees the advice that variables should be declared with some interface, not the implementing class. For example:</p>
  
  <pre><code>List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
  </code></pre>
  
  <p>However, say I am using this list for an algorithm that really depended on the O(1) random access of an <code>ArrayList</code> (e.g. Fisher-Yates shuffling). In that case, the key abstraction that <code>ArrayList</code> represents for me is its array-like nature, not just its List nature. In other words, if someone came along and changed <code>list</code> to a <code>LinkedList</code>, this would be problematic, even though the code would compile. In such a case, is it considered ok to have the declaration use the implementation type?, e.g.:</p>
  
  <pre><code>ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;(); 
  </code></pre>