4

I have a Foo class that extends AbstractList and implements List. This class implements some of the List methods, but some just throw UnsupportedOperationException.

toArray is one of the later and while the compiler doesn't complain about other not really implemented methods, it complains about the toArray with error:

Class must either be declared abstract or implement abstract method toArray(T[]) in List.

public class Foo extends AbstractList implementst List  {
    ...
     public <T> T[] toArray(T[] a) throws UnsupportedOperationException {
        throw new UnsupportedOperationException(error);
    }
}

What is wrong here and why the compiler still thinks the toArray(T[]) method is not implemented?

parsecer
  • 4,758
  • 13
  • 71
  • 140

2 Answers2

2

Since you are using generic method public T[] toArray(T[] a), you should add parameter to class signature and make it extend and implement parameterized class and interface respectively, not the raw ones. Then it will compile:

public class Foo<T> extends AbstractList<T> implements List<T> {

    @Override
    public <E> E[] toArray(E[] a) throws UnsupportedOperationException {
        throw new UnsupportedOperationException("Error!");
    }

    ...
}
Gyrotank
  • 153
  • 1
  • 2
  • 9
  • Agree. Edited the answer. This way there won't be a hiding warning. – Gyrotank Apr 01 '17 at 20:11
  • @azurefrog I wasn't addressing you. – xehpuk Apr 01 '17 at 20:12
  • 1
    This is somewhat unclear - the OP's code already has a parameter in the method signature. – Oliver Charlesworth Apr 01 '17 at 20:33
  • Sorry, meant to write 'class signature', not 'method'. Edited. – Gyrotank Apr 01 '17 at 20:39
  • java.util.Collection is actually a parameterized interface: `public interface Collection extends Iterable`. All its descendants are parameterized, too. You can use raw versions, which treat their contents as Objects (it was needed to provide compatibility with pre-Java 5 classes), but in order to override parameterized method, you need to make your class parameterized, too. – Gyrotank Apr 01 '17 at 22:26
  • If you write it like this: `public class Foo extends AbstractList implements List { public E[] toArray(E[] a) throws UnsupportedOperationException {...}`, you will get an error: "Name clash: The method toArray(E[]) of type Foo has the same erasure as toArray(Object[]) of type AbstractCollection but does not override it". It's explained here: [http://stackoverflow.com/questions/8768028/name-clash-the-method-addobject-of-type-test2-has-the-same-erasure-as-adde](http://stackoverflow.com/questions/8768028/name-clash-the-method-addobject-of-type-test2-has-the-same-erasure-as-adde) – Gyrotank Apr 01 '17 at 22:33
-1

This code compiles :

import java.util.AbstractList;
import java.util.List;

public class Foo extends AbstractList implements List  {

    @Override
    public Object[] toArray(Object[] a) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object get(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }
}

If you use java.util package.

Jay Smith
  • 2,331
  • 3
  • 16
  • 27