Note that there are some explanatory texts on larger screens.

plurals
  1. POgenerics and inheritance question
    text
    copied!<p>I have three classes:</p> <pre><code>public abstract class fileHandler&lt;Key extends Object, Value extends Object&gt; { } public A extends fileHandler&lt;String, String[]&gt; { } public B extends fileHandler&lt;String, String&gt; { } </code></pre> <p>Now in my main function i do something like this:</p> <pre><code>fileHandler file= null; If (&lt;condition&gt;) { fileHandler = new A(); } else fileHandler = new B(): } </code></pre> <p>But this gives 2 compile time errors:</p> <ol> <li>Cannot convert A to fileHandler</li> <li>Cannot convert B to fileHandler</li> </ol> <p>How can I get rid of these errors as I don't get this error if base class is not generic.</p> <h3>Update:</h3> <p>My class hierarchy is:</p> <ol> <li><code>class fileHandler&lt;Key, Value&gt; { }</code> </li> <li><code>class A extends fileHandler&lt;String, String[]&gt; { }</code> </li> <li><code>class B extends fileHandler&lt;String, String&gt; { }</code> </li> <li>class C that calls function <code>gen(object of A)</code> or</li> <li>class D that calls function <code>gen(object of B)</code>. </li> <li>both C and D are derived from abstract class E.</li> </ol> <p>Now how should I define these functions in C and D and E:</p> <p>I gave the following:</p> <h3>E:</h3> <pre><code>public abstract void gen (fileHandler A) throws exception; </code></pre> <h3>C:</h3> <pre><code>void gen (fileHandler A) throws exception; </code></pre> <h3>D:</h3> <pre><code>void gen (fileHandler A) throws exception; </code></pre> <p>C, D, and E give error fileHandler is raw type. Reference to generic type fileHandler(Key, Value) should be parameterized.</p>
 

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