Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are correct in saying "an object that implements an interface has the same type of that interface." And since an object is an instance of a class it can technically never have the same type as an interface since an interface cannot be instantiated in Java. An interface can be thought of as a blueprint for a class. </p> <p>One of the most commonly known implementations that I can think of is in the Java Collections. </p> <pre><code>Map&lt;Integer, String&gt; mapExample = new HashMap&lt;Integer, String&gt;(); </code></pre> <p>Here the Object Type is that of a HashMap and Reference type of Map (The interface). HashMap will inherit the methods that are declared in Map and provide it's own implementation of them.</p> <blockquote> <p>I don't see how an interface in Java determines "the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of that type can be stored." My reasoning is that, because interfaces don't define what the methods can do, they aren't data types and only the classes and primitive types define data types.</p> </blockquote> <p>Let's address this with the following example:</p> <pre><code>public interface IHelloWorld { public String helloWorld(String world); } </code></pre> <p>Here I have an interface which contains a helloWorld method. I have defined the accessor type as <code>public</code>, I have made sure that it will return a type <code>String</code> and it will take a <code>String</code> parameter. So surely it can define what a method can do. Any class that implements this interface will need to provide implementation details for helloWorld. If I were to implement this I would do the following:</p> <pre><code>public class HelloWorld implements IHelloWorld { public String helloWorld(String world) { return "Hello " + world; } } </code></pre> <p>You can see here that although we don't have implementation details in the <code>IHelloWorld</code> interface, we define what the helloWorld method is going to return and what parameter type the method is going to accept.</p> <p>I hope this clarifies things for you.</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.
 

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