Note that there are some explanatory texts on larger screens.

plurals
  1. POCan Class<V> take multiple bounds on the generic type?
    primarykey
    data
    text
    <p>The following code compiles: </p> <pre><code>import java.util.ArrayList; public class ClassTest { private ArrayList&lt;Class&lt;? extends Interface1&gt;&gt; testClass = new ArrayList&lt;Class&lt;? extends Interface1&gt;&gt;(); private interface Interface1 {} } </code></pre> <p>But the following code produces six errors: </p> <pre><code>import java.util.ArrayList; public class ClassTest { private ArrayList&lt;Class&lt;? extends Interface1 &amp; Interface2&gt;&gt; testClass = new ArrayList&lt;Class&lt;? extends Interface1 &amp; Interface2&gt;&gt;(); private interface Interface1 {} private interface Interface2 {} } </code></pre> <p>Errors: </p> <pre><code>ClassTest.java:5: &gt; expected private ArrayList&lt;Class&lt;? extends Interface1 &amp; Interface2&gt;&gt; testClass = ^ ClassTest.java:5: ';' expected private ArrayList&lt;Class&lt;? extends Interface1 &amp; Interface2&gt;&gt; testClass = ^ ClassTest.java:5: &lt;identifier&gt; expected private ArrayList&lt;Class&lt;? extends Interface1 &amp; Interface2&gt;&gt; testClass = ^ ClassTest.java:6: &gt; expected new ArrayList&lt;Class&lt;? extends Interface1 &amp; Interface2&gt;&gt;(); ^ ClassTest.java:6: '(' or '[' expected new ArrayList&lt;Class&lt;? extends Interface1 &amp; Interface2&gt;&gt;(); ^ ClassTest.java:6: illegal start of expression new ArrayList&lt;Class&lt;? extends Interface1 &amp; Interface2&gt;&gt;(); ^ </code></pre> <p>It's pretty clear that the compiler isn't recognizing the <code>&amp;</code> being used for multiple generic bounds on <code>Class</code>. Are you not allowed multiple bounds on the type parameter for <code>Class</code>? What am I supposed to do if I want an <code>ArrayList</code> of <code>Class</code>es that implement both <code>Interface1</code> and <code>Interface2</code>?</p> <p><strong>Motivation:</strong> The code I'm working on at the moment has a lot of declarations like this: </p> <pre><code>private static HashMap&lt;String, Class&lt;? extends ApproximativeMode&gt;&gt; approximativeMethod = new HashMap&lt;String, Class&lt;? extends ApproximativeMode&gt;&gt;(); </code></pre> <p>They map a string to a class which contains a 'method' for doing a certain calculation. Each separate hashmap uses a different wildcard for the classes (so <code>ApproximativeMode</code> might be replaced with <code>ExactMode</code> for the hashmap containing all of the exact methods). </p> <p>I've just gone through all of the methods, making them implement a new interface, <code>NamedMethod</code>, which has a method that gets their full, displayed name (e.g. "Linear hydroxyadenine method"). I now want to put all of the hashmaps into an <code>ArrayList</code> so that I can write an iterator that goes over the hashmaps and displays their full names. So I might end up with: </p> <p><strong>Approximative Methods</strong></p> <p>von Ahsen et al.</p> <p>Owen et al.</p> <p>etc.</p> <p><strong>Exact Methods</strong></p> <p>Santalucia et al. </p> <p>LHM</p> <p>etc.</p> <p>etc.</p> <p>So my declaration of the <code>ArrayList</code> holding all the hashmaps is: </p> <pre><code>private ArrayList&lt;Hashmap&lt;String, Class&lt;? extends NamedMethod&gt;&gt;&gt; namedMethods; </code></pre> <p>Of course, none of the hashmaps are actually of that type, but I was hoping that I could add <code>NamedMethod</code> to the wildcard in each one and get an <code>ArrayList</code> of them all. Clearly that isn't possible. </p> <p>If this isn't clear, let me know and I'll fix it up. </p>
    singulars
    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. 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