Note that there are some explanatory texts on larger screens.

plurals
  1. POType Erasure rears its ugly head, how to circumvent?
    text
    copied!<p>So I have this interface</p> <pre><code>public interface EventHandler&lt;E extends EventObject&gt; { public void handleEvent(E event); } </code></pre> <p>and I want to derive classes for it that handle events, like so:</p> <pre><code>public class WorkingHandler implements EventHandler&lt;MouseEvent&gt; { public void handleEvent(MouseEvent event) { //handle event } } </code></pre> <p>Above example works fine. But compiler won't let me implement EventHandler multiple times, due to the extremely frustrating Type Erasure.</p> <pre><code>public class NotWorkingHandler implements EventHandler&lt;MouseEvent&gt;, EventHandler&lt;KeyEvent&gt; { public void handleEvent(MouseEvent event) { //handle mouse event } public void handleEvent(KeyEvent event) { //handle key event } } </code></pre> <p>Several questions...</p> <ol> <li>Why does Java not allow Type Erasure? IIRC it is something to do with being compatible with earlier versions of Java, correct?</li> </ol> <p>EDIT: I mean why does Java have Type Erasure (had it the other way around) </p> <ol> <li><p>Is there any sort of "mod"/"extension" (for lack of a better word) or programming languages that allow me to get around Type Erasure? As I am not using older versions of Java I don't care about my code being compatible with older code.</p></li> <li><p>Are there any workarounds, within the Java language, that I can use to get around Type Erasure?</p></li> <li><p>If no, what are some alternate ways to code event handling in my program that keeps the compiler happy?</p></li> </ol>
 

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