title: Can you instantiate an Interface in Java
body: <p>Can you instantiate an Interface in Java? I know the quick answer is &quot;no&quot;. But there is something I am not understanding well.</p>
  <p>What is happening here?</p>
  <p><a href="http://developer.android.com/reference/android/content/SharedPreferences.html" rel="nofollow noreferrer">SharedPreferences</a> is a public Interface. However we do not use this interface as I have read about in the books, we do not create a class and implement SharedPreferences. Instead we use this API like this:</p>
  <pre class="lang-java prettyprint-override"><code>SharedPreferences pref = Context.getSharedPreferences(&quot;some value&quot;, 0);
  </code></pre>
  <p>So what is really happening in this code?</p>
  <p>I think its like <a href="https://developer.android.com/reference/android/content/Context.html#getSharedPreferences(java.lang.String,%20int)" rel="nofollow noreferrer">getSharedPreferences()</a> is creating a <code>SharedPreferences</code> object which we can then use and manipulate.</p>
  <p>But <code>SharedPreferences</code> is an Interface ... and I was told you have to implement Interfaces not create object of them. What is this in Java??</p>
  <p>When I look at a Java API and I see a class as defined as Public Interface. How do I know when to implement that interface or when to create this type of object from it?</p>