Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually, you <em>can</em> do what you want. If you want to provide multiple interfaces or a class plus interfaces, you have to have your wildcard look something like this:</p> <pre><code>&lt;T extends ClassA &amp; InterfaceB&gt; </code></pre> <p>See the <a href="http://java.sun.com/docs/books/tutorial/java/generics/index.html" rel="noreferrer">Generics Tutorial</a> at sun.com, specifically the <a href="http://java.sun.com/docs/books/tutorial/java/generics/bounded.html" rel="noreferrer">Bounded Type Parameters</a> section, at the bottom of the page. You can actually list more than one interface if you wish, using <code>&amp; InterfaceName</code> for each one that you need.</p> <p>This can get arbitrarily complicated. To demonstrate, see the JavaDoc declaration of <a href="http://java.sun.com/javase/6/docs/api/java/util/Collections.html#max(java.util.Collection)" rel="noreferrer"><code>Collections#max</code></a>, which (wrapped onto two lines) is:</p> <pre><code>public static &lt;T extends Object &amp; Comparable&lt;? super T&gt;&gt; T max(Collection&lt;? extends T&gt; coll) </code></pre> <p>why so complicated? As said in the Java Generics FAQ: <a href="http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ104" rel="noreferrer">To preserve binary compatibility</a>.</p> <p>It looks like this doesn't work for variable declaration, but it does work when putting a generic boundary on a class. Thus, to do what you want, you may have to jump through a few hoops. But you can do it. You can do something like this, putting a generic boundary on your class and then:</p> <pre><code>class classB { } interface interfaceC { } public class MyClass&lt;T extends classB &amp; interfaceC&gt; { Class&lt;T&gt; variable; } </code></pre> <p>to get <code>variable</code> that has the restriction that you want. For more information and examples, check out page 3 of <a href="http://www.javabeat.net/articles/33-generics-in-java-50-4.html" rel="noreferrer">Generics in Java 5.0</a>. Note, in <code>&lt;T extends B &amp; C&gt;</code>, the class name must come first, and interfaces follow. And of course you can only list a single class.</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. 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