Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When there is a choice between referring to an object by their <code>interface</code> or a <code>class</code>, the former should be preferred, but <em>only if an appropriate type exists</em>.</p> <p>Consider <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html" rel="noreferrer"><code>String</code></a><code>implements</code><a href="http://java.sun.com/javase/6/docs/api/java/lang/CharSequence.html" rel="noreferrer"><code>CharSequence</code></a> as an example. You should not just blindly use <code>CharSequence</code> in preferrence to <code>String</code> for all cases, because that would deny you simple operations like <code>trim()</code>, <code>toUpperCase()</code>, etc.</p> <p>However, a method that takes a <code>String</code> only to care about its sequence of <code>char</code> values <em>should</em> use <code>CharSequence</code> instead, because that is the appropriate type in this case. This is in fact the case with <a href="http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replace%28java.lang.CharSequence,%20java.lang.CharSequence%29" rel="noreferrer"><code>replace(CharSequence target, CharSequence replacement)</code></a> in the <code>String</code> class.</p> <p>Another example is <a href="http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html" rel="noreferrer"><code>java.util.regex.Pattern</code></a> and its <a href="http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#matcher%28java.lang.CharSequence%29" rel="noreferrer"><code>Matcher matcher(CharSequence)</code></a> method. This lets a <code>Matcher</code> be created from <code>Pattern</code> for not just <code>String</code>, but also for all other <code>CharSequence</code> there are out there.</p> <p>A great example in the library of where an <code>interface</code> should've been used, but unfortunately wasn't, can also be found in <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html" rel="noreferrer"><code>Matcher</code></a>: its <a href="http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#appendReplacement%28java.lang.StringBuffer,%20java.lang.String%29" rel="noreferrer"><code>appendReplacement</code></a> and <a href="http://download.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#appendTail%28java.lang.StringBuffer%29" rel="noreferrer"><code>appendTail</code></a> methods accept only <a href="http://download.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html" rel="noreferrer"><code>StringBuffer</code></a>. This class has largely been replaced by its faster cousin <a href="http://download.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html" rel="noreferrer"><code>StringBuilder</code></a> since 1.5.</p> <p>A <code>StringBuilder</code> is not a <code>StringBuffer</code>, so we can not use the former with the <code>append…</code> methods in <code>Matcher</code>. However, both of them <code>implements</code><a href="http://download.oracle.com/javase/6/docs/api/java/lang/Appendable.html" rel="noreferrer"><code>Appendable</code></a> (also introduced in 1.5). Ideally <code>Matcher</code>'s <code>append…</code> method should accept any <code>Appendable</code>, and we would then be able to use <code>StringBuilder</code>, as well as all other <code>Appendable</code> available!</p> <p>So we can see how <em>when an appropriate type exists</em> referring to objects by their interfaces can be a powerful abstraction, but only if those types exist. If the type does not exist, then you may consider defining one of your own if it makes sense. In this <code>Cat</code> example, you may define <code>interface SelfBathable</code>, for example. Then instead of referring to a <code>Cat</code>, you can accept any <code>SelfBathable</code> object (e.g. a <code>Parakeet</code>)</p> <p>If it does not make sense to create a new type, then by all means you can refer to it by its <code>class</code>.</p> <h3>See also</h3> <ul> <li><em>Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces</em> <blockquote> <p>If appropriate interface types exist, then parameters, return values, and fields should all be declared using interface types. If you get into the habit of using interface types, your program will be much more flexible. It is entirely appropriate to refer to an object by a class if no appropriate interface exists.</p> </blockquote></li> </ul> <h3>Related links</h3> <ul> <li><a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5066679" rel="noreferrer">Bug ID: 5066679 - <code>java.util.regex.Matcher</code> should make more use of <code>Appendable</code></a></li> </ul>
    singulars
    1. This table or related slice is empty.
    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. 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