<p>How can I make this java generic cast ?</p>

<pre><code>public interface IField {

}


class Field implements IField { // package private class

}

public class Form {
  private List&lt;Field&gt; fields;


  public List&lt;IField&gt; getFields() {
    return this.fields;

  }

}
</code></pre>

<p>The return statement throws a compiler error (I know the reason - I read the generics tutorial) but it would be very handy to write such code.</p>

<p>If I declared "fields" as List I would need to use a lot of casts to Field in other methods of a Form class .</p>

<p>Can I force that damn compiler to bend it's rules and compile that return statement ?</p>

<p>Thanks in advance.</p>

As it happens, you can, because Java generics are just grafted on, not part of the type system.
You can do
```
```
return (List<IField>)(Object)this.fields;
```
```
because all
```
List<T>
```
objects are the same underlying type.
Bear in mind that this allows anyone to put any type implementing
```
IField
```
in your list, so if your collection is not read-only, you may run into difficulties.