<p>I have the concept of <code>NodeType</code>s and <code>Node</code>s. A <code>NodeType</code> is a bunch of meta-data which you can create <code>Node</code> instances from (a lot like the whole Class / Object relationship).</p>

<p>I have various <code>NodeType</code> implementations and various Node implementations. </p>

<p>In my AbstractNodeType (top level for NodeTypes) I have ab abstract <code>createInstance()</code> method that will, once implemented by the subclass, creates the correct Node instance:</p>

<pre><code>public abstract class AbstractNodeType {
  // ..

  public abstract &lt;T extends AbstractNode&gt; T createInstance();
}
</code></pre>

<p>In my <code>NodeType</code> implementations I implement the method like this:</p>

<pre><code>public class ThingType {
  // ..

  public Thing createInstance() {
    return new Thing(/* .. */);
  }
}

// FYI
public class Thing extends AbstractNode { /* .. */ }
</code></pre>

<p>This is all well and good, but <code>public Thing createInstance()</code> creates a warning about type safety. Specifically:</p>

<blockquote>
  <p>Type safety: The return type Thing for
  createInstance() from the type
  ThingType needs unchecked conversion
  to conform to T from the type
  AbstractNodeType</p>
</blockquote>

<p><strong>What am I doing wrong to cause such a warning?</strong></p>

<p><strong>How can I re-factor my code to fix this?</strong></p>

<p><em><code>@SuppressWarnings("unchecked")</code> is not good, I wish to fix this by coding it correctly, not ignoring the problem!</em></p>

[{'answer_id': 81742, 'author': 'Hank Gay', 'author_id': 4203, 'author_profile': 'https://Stackoverflow.com/users/4203', 'pm_score': 3, 'selected': True, 'text': '<p>You can just replace <code>&lt;T extends AbstractNode&gt; T</code> with <code>AbstractNode</code> thanks to the magic of <a href="http://en.wikipedia.org/wiki/Covariant_return_type" rel="nofollow noreferrer">covariant returns</a>. <code>Java 5</code> added support, but it didn\'t receive the pub it deserved.</p>\n'}, {'answer_id': 81796, 'author': 'jrudolph', 'author_id': 7647, 'author_profile': 'https://Stackoverflow.com/users/7647', 'pm_score': 1, 'selected': False, 'text': '<p>Something like that should work:</p>\n\n<pre><code>interface Node{\n}\ninterface NodeType&lt;T extends Node&gt;{\n    T createInstance();\n}\nclass Thing implements Node{}\nclass ThingType implements NodeType&lt;Thing&gt;{\n    public Thing createInstance() {\n        return new Thing();\n    }\n}\nclass UberThing extends Thing{}\nclass UberThingType extends ThingType{\n    @Override\n    public UberThing createInstance() {\n        return new UberThing();\n    }\n}\n</code></pre>\n'}, {'answer_id': 81825, 'author': 'UlfJack', 'author_id': 15551, 'author_profile': 'https://Stackoverflow.com/users/15551', 'pm_score': 2, 'selected': False, 'text': "<p>Two ways:</p>\n\n<p>(a) Don't use generics. It's probably not necessary in this case. (Although that depends on the code you havn't shown.)</p>\n\n<p>(b) Generify AbstractNodeType as follows:</p>\n\n<pre><code>public abstract class AbstractNodeType&lt;T extends AbstractNode&gt; {\n  public abstract T createInstance();\n}\npublic class ThingType&lt;Thing&gt; {\n  public Thing createInstance() {\n    return new Thing(...);\n  }\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/81723', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1666/']