Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - generics & wildcards & interface versus implementation
    primarykey
    data
    text
    <p>I have a question about Java generics. Say I have the following interface:</p> <pre><code>public static class Something&lt;T&gt; { public void set(T t) { } } public static interface Manager&lt;T&gt; { public void add(final String key, final Something&lt;T&gt; o); public Something&lt;T&gt; get(final String key); } </code></pre> <p>An example of usage:</p> <pre><code>final Manager&lt;Number&gt; m = ...; m.add("key", new Something&lt;Number&gt;()); m.get("key").set(new Integer(5)); </code></pre> <p>I would also like to be able to add <code>Something&lt;Integer&gt;</code>, <code>Something&lt;Double&gt;</code>, ... to the a <code>Manager&lt;Number&gt;</code>. I would say I have to change the signature of the add-function:</p> <pre><code>public static interface Manager&lt;T&gt; { public void add(final String key, final Something&lt;? extends T&gt; o); public Something&lt;T&gt; get(final String key); } final Manager&lt;Number&gt; m = ...; m.add("key", new Something&lt;Integer&gt;()); m.get("key").set(new Integer(5)); </code></pre> <p>So far, so good. Let's look at a possible implementation of the manager:</p> <pre><code>public static class ManagerImplementation&lt;T&gt; implements Manager&lt;T&gt; { protected HashMap&lt;String, Something&lt;T&gt;&gt; map = new HashMap&lt;String, Something&lt;T&gt;&gt;(); public void add(final String key, final Something&lt;? extends T&gt; o) { map.put(key, o); // &lt;--- here } public Something&lt;T&gt; get(final String key) { return map.get(key); } } </code></pre> <p>This fails, since you cannot add a <code>Something&lt;? extends T&gt;</code> to a <code>Map&lt;X, Something&lt;T&gt;&gt;</code>. So let's change this:</p> <pre><code>public static class ManagerImplementation&lt;T&gt; implements Manager&lt;T&gt; { protected HashMap&lt;String, Something&lt;? extends T&gt;&gt; map = new HashMap&lt;String, Something&lt;? extends T&gt;&gt;(); public void add(final String key, final Something&lt;? extends T&gt; o) { map.put(key, o); } public Something&lt;T&gt; get(final String key) { return map.get(key); // &lt;--- here } } </code></pre> <p>This fails since <code>map.get(key)</code> returns <code>Something&lt;? extends T&gt;</code> while the get-function is required to return <code>Something&lt;T&gt;</code>, as defined in the interface.</p> <p>What is the common approach to solve this?</p> <p>Thanks!</p>
    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.
 

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