0

I have following interface:

public interface IMyList<T> {
    <S> S[] toArray(S[] a);
}

I have compilation error in Eclipse for this interface implementation.

public class MyList implements IMyList{
    @Override
    public <S> S[] toArray(S[] a) {
        return null;
    }
}

Please, could you explain me this behaviour?

Thanks.

  • 2
    `IMyList` is used as a raw-type, which is the cause of the error. If you have `class MyList implements IMyList` it compiles fine. – Tunaki Mar 19 '16 at 18:25
  • A raw type is the name of a generic class or interface without any type arguments. I expect warning message, but i have compilation error that says "The method toArray(S[]) of type MyList must override or implement a supertype method". Could you explain this? – Marat Kamalov Mar 19 '16 at 18:28
  • You can also make it compile by getting rid of the `` in `IMyList` - it isn't used anyway. – Paul Boddington Mar 19 '16 at 18:28
  • Thanks, Now it's clear. I have in interface because it is used as raw-type even for method that doesn't use T, public Object[] toArray(Object[] a) { return null; } – Marat Kamalov Mar 19 '16 at 18:30
  • 1
    @MaratKamalov The error messages you get when you mix raw types with generics are almost impossible to understand. Some of the things that are allowed were only allowed to aid the transition from non-generic to generic code and make almost no sense in 2016 now that everybody uses generics. There's no point worrying about it. Just remember not to use raw types. – Paul Boddington Mar 19 '16 at 18:32

0 Answers0