Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By declaring the method</p> <pre><code>public static Formatter&lt;? extends AbstractItem&gt; create() </code></pre> <p>you are declaring that the caller of this method will never know the exact type of the <code>Formatter</code>; the caller will only know that it is some <code>Formatter&lt;X&gt;</code> where <code>X</code> is <code>AbstractItem</code> or a subclass of it. So you can’t pass any instance to the returned <code>Formatter</code> as you never know which <code>X</code> the formatter can handle.</p> <p>Suppressing the warning here is absolutely wrong, the compiler tells you the right thing: your code is unsafe. Given two subclasses of <code>AbstractItem</code>, <code>Foo</code> and <code>Bar</code> the factory could return a <code>Formatter&lt;Foo&gt;</code> and you could pass an instance of <code>Bar</code> to its <code>format</code> method.</p> <p>There’s the semantic problem that there is no indicator for what <code>Formatter</code> your factory will return and why. Either you make it a concrete factory returning a <code>Formatter&lt;X&gt;</code> where X is a concrete type instead of <code>? extends …</code> or you have to add a parameter to provide a hint to the factory, which kind of formatter is required, e.g.</p> <pre><code>public static &lt;T extends AbstractItem&gt; Formatter&lt;T&gt; create(Class&lt;T&gt; forItemType) </code></pre> <p>Or you turn it into</p> <pre><code>public static Formatter&lt;AbstractItem&gt; create() </code></pre> <p>specifying that the returned <code>Formatter</code> can handle <em>all</em> kinds of <code>AbstractItem</code>. Just remember that you still can pass any subclass of <code>AbstractItem</code> to a <code>Formatter&lt;AbstractItem&gt;</code> as every instance of a subclass of <code>AbstractItem</code> is still an instance of <code>AbstractItem</code> too, just like in the pre-Generics times.</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.
    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