title: Enforce type safety in the implementation of a generic method
body: <p>let's say i have this generic class which does some work and produces a result:</p>
  
  <pre><code>public abstract class Work&lt;T&gt; {
  
      private T mResult;
  
      public Work(T result) {
          mResult = result;
      }
  
      public abstract &lt;T&gt; void doWork(T result);
  
      public T getResult() {
          return mResult;
      }
  }
  </code></pre>
  
  <p>For the users of this class i want type safety that would look something like this:</p>
  
  <pre><code>Work&lt;MyResult&gt; work = new Work&lt;MyResult&gt;(new MyResult()){
      public void work(MyResult result){
          //...
      }
  }
  </code></pre>
  
  <p>The problem is Java doesn't work this way and forces me to cast from the generic type:</p>
  
  <pre><code>Work&lt;MyResult&gt; work = new Work&lt;MyResult&gt;(new MyResult()){
      public &lt;T&gt; void work (T result){
          // (MyResult)result - not nice
      }
  }
  </code></pre>
  
  <p>Is there a way to have type safety in a nice way like in the first example?</p>
  
  <p>Thanks,
  Teo</p>