Adam Martinu
Adam Martinu

Reputation: 253

Defining generics from supertype at runtime when overriding methods in Eclipse

I have a class called AbstractCollection<E> and a subclass called AbstractMap<K, V>. The subclass is defined as public abstract class AbstractMap<K, V> extends AbstractCollection<Pair<K, V>>. A method called append in AbstractCollection<E> has the following signature protected abstract int append(E element). But when I override this method in the AbstractMap class public int append(Pair<K, V> pair), I get an error. None of the generics are wild-cards, since they are all specified when an instance of AbstractMap is created as K and V, and therefore I don't understand why this error pops up.

I also tried to let Eclipse override the method for me, but it writes the same signature as I did, and so the error is still there. Right-click on code >> Source >> Override/Implement Methods...

UPDATE I've recreated the code that produce the error in the simplest form:

public abstract class AbstractCollection<E> {

    protected abstract int append(E element);
}

public abstract class AbstractMap<K, V> extends AbstractCollection<Pair<K, V>> {

    @Override
    public int append(Pair<K, V> pair) {  //error pops up here
        //... 
    }

    public static final class Pair<K, V> {

    }
}

I also tried this again on another version of Eclipse, and now it works, although the error flag still appears on the version I mainly use (Luna with java 8).

The error flag contains this message:

The method append(AbstractMap.Pair) of type AbstractMap must override or implement a supertype method

Upvotes: 1

Views: 91

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280172

This may be a problem with how Eclipse reports errors.

What it should have reported first is

public abstract class AbstractMap<K, V> extends AbstractCollection<Pair<K, V>> {
//                                                                 ^ undefined at this point

When declaring this class and its supertype, the type Pair isn't in lexical scope. You need to import it.

import com.example.AbstractMap.Pair;

or use its fully qualified name

public abstract class AbstractMap<K, V> extends AbstractCollection<com.example.AbstractMap.Pair<K, V>> {

Upvotes: 1

Related Questions