Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you instantiate a generic type with different type arguments, the two instantiations are not type compatible. Different instantiations of a generic type are not type-equivalent in the way that a class that inherits from another class is type-equivalent to that class, even when the type arguments themselves may be related by inheritance. Genericity and polymorphism are two separate language features.</p> <p>Your code is equivalent to the following:</p> <pre><code>// Java defaults unspecified type arguments to Object. fileHandler&lt;Object, Object&gt; file; if (...) { // Error: fileHandler&lt;String, String&gt; is not equivalent to fileHandler&lt;Object, Object&gt; file = new fileHandler&lt;String, String&gt;(); } else { // Error: fileHandler&lt;String, String[]&gt; is not equivalent to fileHandler&lt;Object, Object&gt; file = new fileHandler&lt;String, String[]&gt;(); } </code></pre> <p>If you truly want to isolate a polymorphic interface that is independent of the types used to instantiate your fileHandler class, then I would suggest you use an interface:</p> <pre><code>interface IFileHandler { // If this interface needs to talk about keys and values, it // does so using only java.lang.Object. } public class fileHandler&lt;Key extends Object, Value extends Object&gt; implements IFileHandler { // ... } public class A extends fileHandler&lt;String, String&gt; { // ... } public class B extends fileHandler&lt;String, String[]&gt; { // ... } IFileHandler file; if (...) { file = new A(); } else { file = new B(); } </code></pre>
 

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