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?

1
  • toArray is already implemented in AbstractCollection which AbstractList extends. Commented Apr 1, 2017 at 19:49

2 Answers 2

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!");
    }

    ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

Agree. Edited the answer. This way there won't be a hiding warning.
@azurefrog I wasn't addressing you.
This is somewhat unclear - the OP's code already has a parameter in the method signature.
Sorry, meant to write 'class signature', not 'method'. Edited.
java.util.Collection is actually a parameterized interface: public interface Collection<E> extends Iterable<E>. 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.
|
-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.

3 Comments

Ok, but why not <T> T[] toArray(T[] a)?
Because parameter type in super class and subclass can't be different. Here it is docs.oracle.com/javase/tutorial/java/generics/inheritance.html
I'm not sure the relevance of that link - it's simply saying that e.g. SomeClass<Derived> is not a sub-type of SomeClass<Base>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.