Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Option 1 is considered programming to an <em>interface</em>, where option 2 is programming to an <em>implementation</em>. The latter is sometimes necessary, but the former provides you with the ability to easily switch implementations by ensuring that you don't depend on methods provided by a particular implementation.</p> <p>Furthermore, if you create methods that need only the functionality provided by an interface then they should be declared as requiring the interface so that any object implementing the interface can be passed to them. Doing so broadens the scope for re-use of the API. For example:</p> <pre><code>// This can be called passing any List public int countItems(List lst, Filter flt) { // iterate list, apply filter, and count matching objects } // This can called passing only an ArrayList, an unnecessary limitation in this case public int countItems(ArrayList lst, Filter flt) { // iterate list, apply filter, and count matching objects } </code></pre> <p>That said, for some interfaces, there are hidden implementation dependent traps (at least in Java). An example of this in <code>List.get(int)</code>; if you have an <code>ArrayList</code> this is efficient, but for a <code>LinkedList</code> it is not. If the list is very large the difference can be dramatic, esp for a poorly conceived construct like this loop:</p> <pre><code>for(int xa=0,len=list.length; xa&lt;len; xa++) { Object obj=list.get(xa); obj.doSomething(); } </code></pre> <p>which has terrible performance for large linked lists since the list must be traversed from the beginning for every <code>get(xa)</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload